home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Python / ceval.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  64KB  |  3,063 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Execute compiled code */
  33.  
  34. /* XXX TO DO:
  35.    XXX how to pass arguments to call_trace?
  36.    XXX totally get rid of access stuff
  37.    XXX speed up searching for keywords by using a dictionary
  38.    XXX document it!
  39.    */
  40.  
  41. #include "allobjects.h"
  42.  
  43. #include "compile.h"
  44. #include "frameobject.h"
  45. #include "eval.h"
  46. #include "opcode.h"
  47. #include "graminit.h"
  48.  
  49. #include <ctype.h>
  50.  
  51. /* Turn this on if your compiler chokes on the big switch: */
  52. /* #define CASE_TOO_BIG 1 */
  53.  
  54. #ifdef DEBUG
  55. /* For debugging the interpreter: */
  56. #define LLTRACE  1    /* Low-level trace feature */
  57. #define CHECKEXC 1    /* Double-check exception checking */
  58. #endif
  59.  
  60.  
  61. /* Forward declarations */
  62.  
  63. static object *eval_code2 PROTO((codeobject *,
  64.                  object *, object *,
  65.                  object **, int,
  66.                  object **, int,
  67.                  object **, int,
  68.                  object *));
  69. #ifdef LLTRACE
  70. static int prtrace PROTO((object *, char *));
  71. #endif
  72. static void call_exc_trace PROTO((object **, object**, frameobject *));
  73. static int call_trace
  74.     PROTO((object **, object **, frameobject *, char *, object *));
  75. static object *add PROTO((object *, object *));
  76. static object *sub PROTO((object *, object *));
  77. static object *powerop PROTO((object *, object *));
  78. static object *mul PROTO((object *, object *));
  79. static object *divide PROTO((object *, object *));
  80. static object *mod PROTO((object *, object *));
  81. static object *neg PROTO((object *));
  82. static object *pos PROTO((object *));
  83. static object *not PROTO((object *));
  84. static object *invert PROTO((object *));
  85. static object *lshift PROTO((object *, object *));
  86. static object *rshift PROTO((object *, object *));
  87. static object *and PROTO((object *, object *));
  88. static object *xor PROTO((object *, object *));
  89. static object *or PROTO((object *, object *));
  90. static object *call_builtin PROTO((object *, object *, object *));
  91. static object *call_function PROTO((object *, object *, object *));
  92. static object *apply_subscript PROTO((object *, object *));
  93. static object *loop_subscript PROTO((object *, object *));
  94. static int slice_index PROTO((object *, int, int *));
  95. static object *apply_slice PROTO((object *, object *, object *));
  96. static object *build_slice PROTO((object *, object *, object *));
  97. static int assign_subscript PROTO((object *, object *, object *));
  98. static int assign_slice PROTO((object *, object *, object *, object *));
  99. static int cmp_exception PROTO((object *, object *));
  100. static int cmp_member PROTO((object *, object *));
  101. static object *cmp_outcome PROTO((int, object *, object *));
  102. static int import_from PROTO((object *, object *, object *));
  103. static object *build_class PROTO((object *, object *, object *));
  104. #ifdef SUPPORT_OBSOLETE_ACCESS
  105. static int access_statement PROTO((object *, object *, frameobject *));
  106. #endif
  107. static int exec_statement PROTO((object *, object *, object *));
  108. static object *find_from_args PROTO((frameobject *, int));
  109.  
  110.  
  111. /* Pointer to current frame, used to link new frames to */
  112.  
  113. static frameobject *current_frame;
  114.  
  115. #ifdef WITH_THREAD
  116.  
  117. #include <errno.h>
  118. #include "thread.h"
  119.  
  120. static type_lock interpreter_lock = 0;
  121. static long main_thread = 0;
  122.  
  123. void
  124. init_save_thread()
  125. {
  126.     if (interpreter_lock)
  127.         return;
  128.     interpreter_lock = allocate_lock();
  129.     acquire_lock(interpreter_lock, 1);
  130.     main_thread = get_thread_ident();
  131. }
  132.  
  133. #endif
  134.  
  135. /* Functions save_thread and restore_thread are always defined so
  136.    dynamically loaded modules needn't be compiled separately for use
  137.    with and without threads: */
  138.  
  139. object *
  140. save_thread()
  141. {
  142. #ifdef WITH_THREAD
  143.     if (interpreter_lock) {
  144.         object *res;
  145.         res = (object *)current_frame;
  146.         current_frame = NULL;
  147.         release_lock(interpreter_lock);
  148.         return res;
  149.     }
  150. #endif
  151.     return NULL;
  152. }
  153.  
  154. void
  155. restore_thread(x)
  156.     object *x;
  157. {
  158. #ifdef WITH_THREAD
  159.     if (interpreter_lock) {
  160.         int err;
  161.         err = errno;
  162.         acquire_lock(interpreter_lock, 1);
  163.         errno = err;
  164.         current_frame = (frameobject *)x;
  165.     }
  166. #endif
  167. }
  168.  
  169.  
  170. /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
  171.    signal handlers or Mac I/O completion routines) can schedule calls
  172.    to a function to be called synchronously.
  173.    The synchronous function is called with one void* argument.
  174.    It should return 0 for success or -1 for failure -- failure should
  175.    be accompanied by an exception.
  176.  
  177.    If registry succeeds, the registry function returns 0; if it fails
  178.    (e.g. due to too many pending calls) it returns -1 (without setting
  179.    an exception condition).
  180.  
  181.    Note that because registry may occur from within signal handlers,
  182.    or other asynchronous events, calling malloc() is unsafe!
  183.  
  184. #ifdef WITH_THREAD
  185.    Any thread can schedule pending calls, but only the main thread
  186.    will execute them.
  187. #endif
  188.  
  189.    XXX WARNING!  ASYNCHRONOUSLY EXECUTING CODE!
  190.    There are two possible race conditions:
  191.    (1) nested asynchronous registry calls;
  192.    (2) registry calls made while pending calls are being processed.
  193.    While (1) is very unlikely, (2) is a real possibility.
  194.    The current code is safe against (2), but not against (1).
  195.    The safety against (2) is derived from the fact that only one
  196.    thread (the main thread) ever takes things out of the queue.
  197. */
  198.  
  199. static int ticker = 0; /* main loop counter to do periodic things */
  200.  
  201. #define NPENDINGCALLS 32
  202. static struct {
  203.     int (*func) PROTO((ANY *));
  204.     ANY *arg;
  205. } pendingcalls[NPENDINGCALLS];
  206. static volatile int pendingfirst = 0;
  207. static volatile int pendinglast = 0;
  208.  
  209. int
  210. Py_AddPendingCall(func, arg)
  211.     int (*func) PROTO((ANY *));
  212.     ANY *arg;
  213. {
  214.     static int busy = 0;
  215.     int i, j;
  216.     /* XXX Begin critical section */
  217.     /* XXX If you want this to be safe against nested
  218.        XXX asynchronous calls, you'll have to work harder! */
  219.     if (busy)
  220.         return -1;
  221.     busy = 1;
  222.     i = pendinglast;
  223.     j = (i + 1) % NPENDINGCALLS;
  224.     if (j == pendingfirst)
  225.         return -1; /* Queue full */
  226.     pendingcalls[i].func = func;
  227.     pendingcalls[i].arg = arg;
  228.     pendinglast = j;
  229.     ticker = 0; /* Signal main loop */
  230.     busy = 0;
  231.     /* XXX End critical section */
  232.     return 0;
  233. }
  234.  
  235. int
  236. Py_MakePendingCalls()
  237. {
  238.     static int busy = 0;
  239. #ifdef WITH_THREAD
  240.     if (get_thread_ident() != main_thread) {
  241.         ticker = 0; /* We're not done yet */
  242.         return 0;
  243.     }
  244. #endif
  245.     if (busy) {
  246.         ticker = 0; /* We're not done yet */
  247.         return 0;
  248.     }
  249.     busy = 1;
  250.     for (;;) {
  251.         int i;
  252.         int (*func) PROTO((ANY *));
  253.         ANY *arg;
  254.         i = pendingfirst;
  255.         if (i == pendinglast)
  256.             break; /* Queue empty */
  257.         func = pendingcalls[i].func;
  258.         arg = pendingcalls[i].arg;
  259.         pendingfirst = (i + 1) % NPENDINGCALLS;
  260.         if (func(arg) < 0) {
  261.             busy = 0;
  262.             ticker = 0; /* We're not done yet */
  263.             return -1;
  264.         }
  265.     }
  266.     busy = 0;
  267.     return 0;
  268. }
  269.  
  270.  
  271. /* Status code for main loop (reason for stack unwind) */
  272.  
  273. enum why_code {
  274.         WHY_NOT,    /* No error */
  275.         WHY_EXCEPTION,    /* Exception occurred */
  276.         WHY_RERAISE,    /* Exception re-raised by 'finally' */
  277.         WHY_RETURN,    /* 'return' statement */
  278.         WHY_BREAK    /* 'break' statement */
  279. };
  280.  
  281.  
  282. /* Backward compatible interface */
  283.  
  284. object *
  285. eval_code(co, globals, locals)
  286.     codeobject *co;
  287.     object *globals;
  288.     object *locals;
  289. {
  290.     return eval_code2(co,
  291.               globals, locals,
  292.               (object **)NULL, 0,
  293.               (object **)NULL, 0,
  294.               (object **)NULL, 0,
  295.               (object *)NULL);
  296. }
  297.  
  298.  
  299. /* Interpreter main loop */
  300.  
  301. #ifndef MAX_RECURSION_DEPTH
  302. #define MAX_RECURSION_DEPTH 10000
  303. #endif
  304.  
  305. static int recursion_depth = 0;
  306.  
  307. static object *
  308. eval_code2(co, globals, locals,
  309.        args, argcount, kws, kwcount, defs, defcount, owner)
  310.     codeobject *co;
  311.     object *globals;
  312.     object *locals;
  313.     object **args;
  314.     int argcount;
  315.     object **kws; /* length: 2*kwcount */
  316.     int kwcount;
  317.     object **defs;
  318.     int defcount;
  319.     object *owner;
  320. {
  321.     register unsigned char *next_instr;
  322.     register int opcode;    /* Current opcode */
  323.     register int oparg;    /* Current opcode argument, if any */
  324.     register object **stack_pointer;
  325.     register enum why_code why; /* Reason for block stack unwind */
  326.     register int err;    /* Error status -- nonzero if error */
  327.     register object *x;    /* Result object -- NULL if error */
  328.     register object *v;    /* Temporary objects popped off stack */
  329.     register object *w;
  330.     register object *u;
  331.     register object *t;
  332.     register frameobject *f; /* Current frame */
  333.     register object **fastlocals;
  334.     object *retval;        /* Return value */
  335. #ifdef SUPPORT_OBSOLETE_ACCESS
  336.     int defmode = 0;    /* Default access mode for new variables */
  337. #endif
  338. #ifdef LLTRACE
  339.     int lltrace;
  340. #endif
  341. #if defined(DEBUG) || defined(LLTRACE)
  342.     /* Make it easier to find out where we are with a debugger */
  343.     char *filename = getstringvalue(co->co_filename);
  344. #endif
  345.  
  346. /* Code access macros */
  347.  
  348. #define GETCONST(i)    Getconst(f, i)
  349. #define GETNAME(i)    Getname(f, i)
  350. #define GETNAMEV(i)    Getnamev(f, i)
  351. #define FIRST_INSTR()    (GETUSTRINGVALUE(f->f_code->co_code))
  352. #define INSTR_OFFSET()    (next_instr - FIRST_INSTR())
  353. #define NEXTOP()    (*next_instr++)
  354. #define NEXTARG()    (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
  355. #define JUMPTO(x)    (next_instr = FIRST_INSTR() + (x))
  356. #define JUMPBY(x)    (next_instr += (x))
  357.  
  358. /* Stack manipulation macros */
  359.  
  360. #define STACK_LEVEL()    (stack_pointer - f->f_valuestack)
  361. #define EMPTY()        (STACK_LEVEL() == 0)
  362. #define TOP()        (stack_pointer[-1])
  363. #define BASIC_PUSH(v)    (*stack_pointer++ = (v))
  364. #define BASIC_POP()    (*--stack_pointer)
  365.  
  366. #define CHECK_STACK(n)    (STACK_LEVEL() + (n) < f->f_nvalues || \
  367.               (stack_pointer = extend_stack(f, STACK_LEVEL(), n)))
  368.  
  369. #ifdef LLTRACE
  370. #define PUSH(v)        (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
  371. #define POP()        (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
  372. #else
  373. #define PUSH(v)        BASIC_PUSH(v)
  374. #define POP()        BASIC_POP()
  375. #endif
  376.  
  377. /* Local variable macros */
  378.  
  379. #define GETLOCAL(i)    (fastlocals[i])
  380. #define SETLOCAL(i, value)    do { XDECREF(GETLOCAL(i)); \
  381.                      GETLOCAL(i) = value; } while (0)
  382.  
  383. #ifdef USE_STACKCHECK
  384.     if (recursion_depth%10 == 0 && PyOS_CheckStack()) {
  385.         err_setstr(MemoryError, "Stack overflow");
  386.         return NULL;
  387.     }
  388. #endif
  389.  
  390.     if (globals == NULL) {
  391.         err_setstr(SystemError, "eval_code2: NULL globals");
  392.         return NULL;
  393.     }
  394.  
  395. #ifdef LLTRACE
  396.     lltrace = dictlookup(globals, "__lltrace__") != NULL;
  397. #endif
  398.  
  399.     f = newframeobject(
  400.             current_frame,        /*back*/
  401.             co,            /*code*/
  402.             globals,        /*globals*/
  403.             locals,            /*locals*/
  404.             owner,            /*owner*/
  405.             50,            /*nvalues*/
  406.             20);            /*nblocks*/
  407.     if (f == NULL)
  408.         return NULL;
  409.  
  410.     current_frame = f;
  411.  
  412.     if (co->co_nlocals > 0)
  413.         fastlocals = ((listobject *)f->f_fastlocals)->ob_item;
  414.  
  415.     if (co->co_argcount > 0 ||
  416.         co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
  417.         int i;
  418.         int n = argcount;
  419.         object *kwdict = NULL;
  420.         if (co->co_flags & CO_VARKEYWORDS) {
  421.             kwdict = newmappingobject();
  422.             if (kwdict == NULL)
  423.                 goto fail;
  424.         }
  425.         if (argcount > co->co_argcount) {
  426.             if (!(co->co_flags & CO_VARARGS)) {
  427.                 err_setstr(TypeError, "too many arguments");
  428.                 goto fail;
  429.             }
  430.             n = co->co_argcount;
  431.         }
  432.         for (i = 0; i < n; i++) {
  433.             x = args[i];
  434.             INCREF(x);
  435.             SETLOCAL(i, x);
  436.         }
  437.         if (co->co_flags & CO_VARARGS) {
  438.             u = newtupleobject(argcount - n);
  439.             for (i = n; i < argcount; i++) {
  440.                 x = args[i];
  441.                 INCREF(x);
  442.                 SETTUPLEITEM(u, i-n, x);
  443.             }
  444.             SETLOCAL(co->co_argcount, u);
  445.         }
  446.         for (i = 0; i < kwcount; i++) {
  447.             object *keyword = kws[2*i];
  448.             object *value = kws[2*i + 1];
  449.             int j;
  450.             /* XXX slow -- speed up using dictionary? */
  451.             for (j = 0; j < co->co_argcount; j++) {
  452.                 object *nm = GETTUPLEITEM(co->co_varnames, j);
  453.                 if (cmpobject(keyword, nm) == 0)
  454.                     break;
  455.             }
  456.             if (j >= co->co_argcount) {
  457.                 if (kwdict == NULL) {
  458.                     err_setval(TypeError, keyword);
  459.                     goto fail;
  460.                 }
  461.                 mappinginsert(kwdict, keyword, value);
  462.             }
  463.             else {
  464.                 if (GETLOCAL(j) != NULL) {
  465.                     err_setstr(TypeError,
  466.                         "keyword parameter redefined");
  467.                     goto fail;
  468.                 }
  469.                 INCREF(value);
  470.                 SETLOCAL(j, value);
  471.             }
  472.         }
  473.         if (argcount < co->co_argcount) {
  474.             int m = co->co_argcount - defcount;
  475.             for (i = argcount; i < m; i++) {
  476.                 if (GETLOCAL(i) == NULL) {
  477.                     err_setstr(TypeError,
  478.                            "not enough arguments");
  479.                     goto fail;
  480.                 }
  481.             }
  482.             if (n > m)
  483.                 i = n - m;
  484.             else
  485.                 i = 0;
  486.             for (; i < defcount; i++) {
  487.                 if (GETLOCAL(m+i) == NULL) {
  488.                     object *def = defs[i];
  489.                     INCREF(def);
  490.                     SETLOCAL(m+i, def);
  491.                 }
  492.             }
  493.         }
  494.         if (kwdict != NULL) {
  495.             i = co->co_argcount;
  496.             if (co->co_flags & CO_VARARGS)
  497.                 i++;
  498.             SETLOCAL(i, kwdict);
  499.         }
  500.         if (0) {
  501.      fail:
  502.             XDECREF(kwdict);
  503.             goto fail2;
  504.         }
  505.     }
  506.     else {
  507.         if (argcount > 0 || kwcount > 0) {
  508.             err_setstr(TypeError, "no arguments expected");
  509.  fail2:
  510.             current_frame = f->f_back;
  511.             DECREF(f);
  512.             return NULL;
  513.         }
  514.     }
  515.  
  516.     if (sys_trace != NULL) {
  517.         /* sys_trace, if defined, is a function that will
  518.            be called  on *every* entry to a code block.
  519.            Its return value, if not None, is a function that
  520.            will be called at the start of each executed line
  521.            of code.  (Actually, the function must return
  522.            itself in order to continue tracing.)
  523.            The trace functions are called with three arguments:
  524.            a pointer to the current frame, a string indicating
  525.            why the function is called, and an argument which
  526.            depends on the situation.  The global trace function
  527.            (sys.trace) is also called whenever an exception
  528.            is detected. */
  529.         if (call_trace(&sys_trace, &f->f_trace, f, "call",
  530.                    None/*XXX how to compute arguments now?*/)) {
  531.             /* Trace function raised an error */
  532.             current_frame = f->f_back;
  533.             DECREF(f);
  534.             return NULL;
  535.         }
  536.     }
  537.  
  538.     if (sys_profile != NULL) {
  539.         /* Similar for sys_profile, except it needn't return
  540.            itself and isn't called for "line" events */
  541.         if (call_trace(&sys_profile, (object**)0, f, "call",
  542.                    None/*XXX*/)) {
  543.             current_frame = f->f_back;
  544.             DECREF(f);
  545.             return NULL;
  546.         }
  547.     }
  548.  
  549.     if (++recursion_depth > MAX_RECURSION_DEPTH) {
  550.         --recursion_depth;
  551.         err_setstr(RuntimeError, "Maximum recursion depth exceeded");
  552.         current_frame = f->f_back;
  553.         DECREF(f);
  554.         return NULL;
  555.     }
  556.  
  557.     next_instr = GETUSTRINGVALUE(f->f_code->co_code);
  558.     stack_pointer = f->f_valuestack;
  559.     
  560.     why = WHY_NOT;
  561.     err = 0;
  562.     x = None;    /* Not a reference, just anything non-NULL */
  563.     
  564.     for (;;) {
  565.         /* Do periodic things.
  566.            Doing this every time through the loop would add
  567.            too much overhead (a function call per instruction).
  568.            So we do it only every Nth instruction.
  569.  
  570.            The ticker is reset to zero if there are pending
  571.            calls (see Py_AddPendingCalls() and
  572.            Py_MakePendingCalls() above). */
  573.         
  574.         if (--ticker < 0) {
  575.             ticker = sys_checkinterval;
  576.             if (pendingfirst != pendinglast) {
  577.                 if (Py_MakePendingCalls() < 0) {
  578.                     why = WHY_EXCEPTION;
  579.                     goto on_error;
  580.                 }
  581.             }
  582.             if (sigcheck()) {
  583.                 why = WHY_EXCEPTION;
  584.                 goto on_error;
  585.             }
  586.  
  587. #ifdef WITH_THREAD
  588.             if (interpreter_lock) {
  589.                 /* Give another thread a chance */
  590.  
  591.                 current_frame = NULL;
  592.                 release_lock(interpreter_lock);
  593.  
  594.                 /* Other threads may run now */
  595.  
  596.                 acquire_lock(interpreter_lock, 1);
  597.                 current_frame = f;
  598.             }
  599. #endif
  600.         }
  601.  
  602.         /* Extract opcode and argument */
  603.  
  604. #if defined(DEBUG) || defined(LLTRACE)
  605.         f->f_lasti = INSTR_OFFSET();
  606. #endif
  607.         
  608.         opcode = NEXTOP();
  609.         if (HAS_ARG(opcode))
  610.             oparg = NEXTARG();
  611.  
  612. #ifdef LLTRACE
  613.         /* Instruction tracing */
  614.         
  615.         if (lltrace) {
  616.             if (HAS_ARG(opcode)) {
  617.                 printf("%d: %d, %d\n",
  618.                     (int) (INSTR_OFFSET() - 3),
  619.                     opcode, oparg);
  620.             }
  621.             else {
  622.                 printf("%d: %d\n",
  623.                     (int) (INSTR_OFFSET() - 1), opcode);
  624.             }
  625.         }
  626. #endif
  627.  
  628.         if (!CHECK_STACK(3)) {
  629.             x = NULL;
  630.             break;
  631.         }
  632.  
  633.         /* Main switch on opcode */
  634.         
  635.         switch (opcode) {
  636.         
  637.         /* BEWARE!
  638.            It is essential that any operation that fails sets either
  639.            x to NULL, err to nonzero, or why to anything but WHY_NOT,
  640.            and that no operation that succeeds does this! */
  641.         
  642.         /* case STOP_CODE: this is an error! */
  643.         
  644.         case POP_TOP:
  645.             v = POP();
  646.             DECREF(v);
  647.             break;
  648.         
  649.         case ROT_TWO:
  650.             v = POP();
  651.             w = POP();
  652.             PUSH(v);
  653.             PUSH(w);
  654.             break;
  655.         
  656.         case ROT_THREE:
  657.             v = POP();
  658.             w = POP();
  659.             x = POP();
  660.             PUSH(v);
  661.             PUSH(x);
  662.             PUSH(w);
  663.             break;
  664.         
  665.         case DUP_TOP:
  666.             v = TOP();
  667.             INCREF(v);
  668.             PUSH(v);
  669.             break;
  670.         
  671.         case UNARY_POSITIVE:
  672.             v = POP();
  673.             x = pos(v);
  674.             DECREF(v);
  675.             PUSH(x);
  676.             break;
  677.         
  678.         case UNARY_NEGATIVE:
  679.             v = POP();
  680.             x = neg(v);
  681.             DECREF(v);
  682.             PUSH(x);
  683.             break;
  684.         
  685.         case UNARY_NOT:
  686.             v = POP();
  687.             x = not(v);
  688.             DECREF(v);
  689.             PUSH(x);
  690.             break;
  691.         
  692.         case UNARY_CONVERT:
  693.             v = POP();
  694.             x = reprobject(v);
  695.             DECREF(v);
  696.             PUSH(x);
  697.             break;
  698.             
  699.         case UNARY_INVERT:
  700.             v = POP();
  701.             x = invert(v);
  702.             DECREF(v);
  703.             PUSH(x);
  704.             break;
  705.         
  706.         case BINARY_POWER:
  707.             w = POP();
  708.             v = POP();
  709.             x = powerop(v, w);
  710.             DECREF(v);
  711.             DECREF(w);
  712.             PUSH(x);
  713.             break;
  714.         
  715.         case BINARY_MULTIPLY:
  716.             w = POP();
  717.             v = POP();
  718.             x = mul(v, w);
  719.             DECREF(v);
  720.             DECREF(w);
  721.             PUSH(x);
  722.             break;
  723.         
  724.         case BINARY_DIVIDE:
  725.             w = POP();
  726.             v = POP();
  727.             x = divide(v, w);
  728.             DECREF(v);
  729.             DECREF(w);
  730.             PUSH(x);
  731.             break;
  732.         
  733.         case BINARY_MODULO:
  734.             w = POP();
  735.             v = POP();
  736.             x = mod(v, w);
  737.             DECREF(v);
  738.             DECREF(w);
  739.             PUSH(x);
  740.             break;
  741.         
  742.         case BINARY_ADD:
  743.             w = POP();
  744.             v = POP();
  745.             x = add(v, w);
  746.             DECREF(v);
  747.             DECREF(w);
  748.             PUSH(x);
  749.             break;
  750.         
  751.         case BINARY_SUBTRACT:
  752.             w = POP();
  753.             v = POP();
  754.             x = sub(v, w);
  755.             DECREF(v);
  756.             DECREF(w);
  757.             PUSH(x);
  758.             break;
  759.         
  760.         case BINARY_SUBSCR:
  761.             w = POP();
  762.             v = POP();
  763.             x = apply_subscript(v, w);
  764.             DECREF(v);
  765.             DECREF(w);
  766.             PUSH(x);
  767.             break;
  768.         
  769.         case BINARY_LSHIFT:
  770.             w = POP();
  771.             v = POP();
  772.             x = lshift(v, w);
  773.             DECREF(v);
  774.             DECREF(w);
  775.             PUSH(x);
  776.             break;
  777.         
  778.         case BINARY_RSHIFT:
  779.             w = POP();
  780.             v = POP();
  781.             x = rshift(v, w);
  782.             DECREF(v);
  783.             DECREF(w);
  784.             PUSH(x);
  785.             break;
  786.         
  787.         case BINARY_AND:
  788.             w = POP();
  789.             v = POP();
  790.             x = and(v, w);
  791.             DECREF(v);
  792.             DECREF(w);
  793.             PUSH(x);
  794.             break;
  795.         
  796.         case BINARY_XOR:
  797.             w = POP();
  798.             v = POP();
  799.             x = xor(v, w);
  800.             DECREF(v);
  801.             DECREF(w);
  802.             PUSH(x);
  803.             break;
  804.         
  805.         case BINARY_OR:
  806.             w = POP();
  807.             v = POP();
  808.             x = or(v, w);
  809.             DECREF(v);
  810.             DECREF(w);
  811.             PUSH(x);
  812.             break;
  813.         
  814.         case SLICE+0:
  815.         case SLICE+1:
  816.         case SLICE+2:
  817.         case SLICE+3:
  818.             if ((opcode-SLICE) & 2)
  819.                 w = POP();
  820.             else
  821.                 w = NULL;
  822.             if ((opcode-SLICE) & 1)
  823.                 v = POP();
  824.             else
  825.                 v = NULL;
  826.             u = POP();
  827.             x = apply_slice(u, v, w);
  828.             DECREF(u);
  829.             XDECREF(v);
  830.             XDECREF(w);
  831.             PUSH(x);
  832.             break;
  833.         
  834.         case STORE_SLICE+0:
  835.         case STORE_SLICE+1:
  836.         case STORE_SLICE+2:
  837.         case STORE_SLICE+3:
  838.             if ((opcode-STORE_SLICE) & 2)
  839.                 w = POP();
  840.             else
  841.                 w = NULL;
  842.             if ((opcode-STORE_SLICE) & 1)
  843.                 v = POP();
  844.             else
  845.                 v = NULL;
  846.             u = POP();
  847.             t = POP();
  848.             err = assign_slice(u, v, w, t); /* u[v:w] = t */
  849.             DECREF(t);
  850.             DECREF(u);
  851.             XDECREF(v);
  852.             XDECREF(w);
  853.             break;
  854.         
  855.         case DELETE_SLICE+0:
  856.         case DELETE_SLICE+1:
  857.         case DELETE_SLICE+2:
  858.         case DELETE_SLICE+3:
  859.             if ((opcode-DELETE_SLICE) & 2)
  860.                 w = POP();
  861.             else
  862.                 w = NULL;
  863.             if ((opcode-DELETE_SLICE) & 1)
  864.                 v = POP();
  865.             else
  866.                 v = NULL;
  867.             u = POP();
  868.             err = assign_slice(u, v, w, (object *)NULL);
  869.                             /* del u[v:w] */
  870.             DECREF(u);
  871.             XDECREF(v);
  872.             XDECREF(w);
  873.             break;
  874.         
  875.         case STORE_SUBSCR:
  876.             w = POP();
  877.             v = POP();
  878.             u = POP();
  879.             /* v[w] = u */
  880.             err = assign_subscript(v, w, u);
  881.             DECREF(u);
  882.             DECREF(v);
  883.             DECREF(w);
  884.             break;
  885.         
  886.         case DELETE_SUBSCR:
  887.             w = POP();
  888.             v = POP();
  889.             /* del v[w] */
  890.             err = assign_subscript(v, w, (object *)NULL);
  891.             DECREF(v);
  892.             DECREF(w);
  893.             break;
  894.         
  895.         case PRINT_EXPR:
  896.             v = POP();
  897.             /* Print value except if procedure result */
  898.             /* Before printing, also assign to '_' */
  899.             if (v != None &&
  900.                 (err = dictinsert(f->f_builtins, "_", v)) == 0 &&
  901.                 !suppress_print) {
  902.                 flushline();
  903.                 x = sysget("stdout");
  904.                 err = writeobject(v, x, 0);
  905.                 softspace(x, 1);
  906.                 flushline();
  907.             }
  908.             DECREF(v);
  909.             break;
  910.         
  911.         case PRINT_ITEM:
  912.             v = POP();
  913.             w = sysget("stdout");
  914.             if (softspace(w, 1))
  915.                 writestring(" ", w);
  916.             err = writeobject(v, w, PRINT_RAW);
  917.             if (err == 0 && is_stringobject(v)) {
  918.                 /* XXX move into writeobject() ? */
  919.                 char *s = getstringvalue(v);
  920.                 int len = getstringsize(v);
  921.                 if (len > 0 &&
  922.                     isspace(Py_CHARMASK(s[len-1])) &&
  923.                     s[len-1] != ' ')
  924.                     softspace(w, 0);
  925.             }
  926.             DECREF(v);
  927.             break;
  928.         
  929.         case PRINT_NEWLINE:
  930.             x = sysget("stdout");
  931.             if (x == NULL)
  932.                 err_setstr(RuntimeError, "lost sys.stdout");
  933.             else {
  934.                 writestring("\n", x);
  935.                 softspace(x, 0);
  936.             }
  937.             break;
  938.         
  939.         case BREAK_LOOP:
  940.             why = WHY_BREAK;
  941.             break;
  942.  
  943.         case RAISE_VARARGS:
  944.             u = v = w = NULL;
  945.             switch (oparg) {
  946.             case 3:
  947.                 u = POP(); /* traceback */
  948.                 if (u == None) {
  949.                     DECREF(u);
  950.                     u = NULL;
  951.                 }
  952.                 else if (!PyTraceBack_Check(u)) {
  953.                     err_setstr(TypeError,
  954.                     "raise 3rd arg must be traceback or None");
  955.                     goto raise_error;
  956.                 }
  957.                 /* Fallthrough */
  958.             case 2:
  959.                 v = POP(); /* value */
  960.                 /* Fallthrough */
  961.             case 1:
  962.                 w = POP(); /* exc */
  963.                 break;
  964.             default:
  965.                 err_setstr(SystemError,
  966.                        "bad RAISE_VARARGS oparg");
  967.                 goto raise_error;
  968.             }
  969.             if (v == NULL) {
  970.                 v = None;
  971.                 INCREF(v);
  972.             }
  973.             /* A tuple is equivalent to its first element here */
  974.             while (is_tupleobject(w) && gettuplesize(w) > 0) {
  975.                 t = w;
  976.                 w = GETTUPLEITEM(w, 0);
  977.                 INCREF(w);
  978.                 DECREF(t);
  979.             }
  980.             if (is_stringobject(w)) {
  981.                 ;
  982.             } else if (is_classobject(w)) {
  983.                 if (!is_instanceobject(v)
  984.                     || !issubclass((object*)((instanceobject*)v)->in_class,
  985.                            w)) {
  986.                     err_setstr(TypeError,
  987.                            "a class exception must have a value that is an instance of the class");
  988.                     goto raise_error;
  989.                 }
  990.             } else if (is_instanceobject(w)) {
  991.                 if (v != None) {
  992.                     err_setstr(TypeError,
  993.                            "an instance exception may not have a separate value");
  994.                     goto raise_error;
  995.                 }
  996.                 else {
  997.                     DECREF(v);
  998.                     v = w;
  999.                     w = (object*) ((instanceobject*)w)->in_class;
  1000.                     INCREF(w);
  1001.                 }
  1002.             }
  1003.             else {
  1004.                 err_setstr(TypeError,
  1005.                     "exceptions must be strings, classes, or instances");
  1006.                 goto raise_error;
  1007.             }
  1008.             err_restore(w, v, u);
  1009.             if (u == NULL)
  1010.                 why = WHY_EXCEPTION;
  1011.             else
  1012.                 why = WHY_RERAISE;
  1013.             break;
  1014.         raise_error:
  1015.             XDECREF(v);
  1016.             XDECREF(w);
  1017.             XDECREF(u);
  1018.             why = WHY_EXCEPTION;
  1019.             break;
  1020.         
  1021.         case LOAD_LOCALS:
  1022.             if ((x = f->f_locals) == NULL) {
  1023.                 err_setstr(SystemError, "no locals");
  1024.                 break;
  1025.             }
  1026.             INCREF(x);
  1027.             PUSH(x);
  1028.             break;
  1029.         
  1030.         case RETURN_VALUE:
  1031.             retval = POP();
  1032.             why = WHY_RETURN;
  1033.             break;
  1034.  
  1035.         case EXEC_STMT:
  1036.             w = POP();
  1037.             v = POP();
  1038.             u = POP();
  1039.             err = exec_statement(u, v, w);
  1040.             DECREF(u);
  1041.             DECREF(v);
  1042.             DECREF(w);
  1043.             break;
  1044.         
  1045.         case POP_BLOCK:
  1046.             {
  1047.                 block *b = pop_block(f);
  1048.                 while (STACK_LEVEL() > b->b_level) {
  1049.                     v = POP();
  1050.                     DECREF(v);
  1051.                 }
  1052.             }
  1053.             break;
  1054.         
  1055.         case END_FINALLY:
  1056.             v = POP();
  1057.             if (is_intobject(v)) {
  1058.                 why = (enum why_code) getintvalue(v);
  1059.                 if (why == WHY_RETURN)
  1060.                     retval = POP();
  1061.             }
  1062.             else if (is_stringobject(v) || is_classobject(v)) {
  1063.                 w = POP();
  1064.                 u = POP();
  1065.                 err_restore(v, w, u);
  1066.                 why = WHY_RERAISE;
  1067.                 break;
  1068.             }
  1069.             else if (v != None) {
  1070.                 err_setstr(SystemError,
  1071.                     "'finally' pops bad exception");
  1072.                 why = WHY_EXCEPTION;
  1073.             }
  1074.             DECREF(v);
  1075.             break;
  1076.         
  1077.         case BUILD_CLASS:
  1078.             u = POP();
  1079.             v = POP();
  1080.             w = POP();
  1081.             x = build_class(u, v, w);
  1082.             PUSH(x);
  1083.             DECREF(u);
  1084.             DECREF(v);
  1085.             DECREF(w);
  1086.             break;
  1087.         
  1088.         case STORE_NAME:
  1089.             w = GETNAMEV(oparg);
  1090.             v = POP();
  1091.             if ((x = f->f_locals) == NULL) {
  1092.                 err_setstr(SystemError, "no locals");
  1093.                 break;
  1094.             }
  1095. #ifdef SUPPORT_OBSOLETE_ACCESS
  1096.             u = dict2lookup(x, w);
  1097.             if (u == NULL) {
  1098.                 if (defmode != 0) {
  1099.                     if (v != None)
  1100.                         u = (object *)v->ob_type;
  1101.                     else
  1102.                         u = NULL;
  1103.                     x = newaccessobject(v, x,
  1104.                                 (typeobject *)u,
  1105.                                 defmode);
  1106.                     DECREF(v);
  1107.                     if (x == NULL)
  1108.                         break;
  1109.                     v = x;
  1110.                 }
  1111.             }
  1112.             else if (is_accessobject(u)) {
  1113.                 err = setaccessvalue(u, x, v);
  1114.                 DECREF(v);
  1115.                 break;
  1116.             }
  1117. #endif
  1118.             err = dict2insert(x, w, v);
  1119.             DECREF(v);
  1120.             break;
  1121.         
  1122.         case DELETE_NAME:
  1123.             w = GETNAMEV(oparg);
  1124.             if ((x = f->f_locals) == NULL) {
  1125.                 err_setstr(SystemError, "no locals");
  1126.                 break;
  1127.             }
  1128. #ifdef SUPPORT_OBSOLETE_ACCESS
  1129.             u = dict2lookup(x, w);
  1130.             if (u != NULL && is_accessobject(u)) {
  1131.                 err = setaccessvalue(u, x,
  1132.                              (object *)NULL);
  1133.                 break;
  1134.             }
  1135. #endif
  1136.             if ((err = dict2remove(x, w)) != 0)
  1137.                 err_setval(NameError, w);
  1138.             break;
  1139.  
  1140. #ifdef CASE_TOO_BIG
  1141.         default: switch (opcode) {
  1142. #endif
  1143.         
  1144.         case UNPACK_TUPLE:
  1145.             v = POP();
  1146.             if (!is_tupleobject(v)) {
  1147.                 err_setstr(TypeError, "unpack non-tuple");
  1148.                 why = WHY_EXCEPTION;
  1149.             }
  1150.             else if (gettuplesize(v) != oparg) {
  1151.                 err_setstr(ValueError,
  1152.                     "unpack tuple of wrong size");
  1153.                 why = WHY_EXCEPTION;
  1154.             }
  1155.             else {
  1156.                 if (!CHECK_STACK(oparg)) {
  1157.                     x = NULL;
  1158.                     break;
  1159.                 }
  1160.                 for (; --oparg >= 0; ) {
  1161.                     w = GETTUPLEITEM(v, oparg);
  1162.                     INCREF(w);
  1163.                     PUSH(w);
  1164.                 }
  1165.             }
  1166.             DECREF(v);
  1167.             break;
  1168.         
  1169.         case UNPACK_LIST:
  1170.             v = POP();
  1171.             if (!is_listobject(v)) {
  1172.                 err_setstr(TypeError, "unpack non-list");
  1173.                 why = WHY_EXCEPTION;
  1174.             }
  1175.             else if (getlistsize(v) != oparg) {
  1176.                 err_setstr(ValueError,
  1177.                     "unpack list of wrong size");
  1178.                 why = WHY_EXCEPTION;
  1179.             }
  1180.             else {
  1181.                 if (!CHECK_STACK(oparg)) {
  1182.                     x = NULL;
  1183.                     break;
  1184.                 }
  1185.                 for (; --oparg >= 0; ) {
  1186.                     w = getlistitem(v, oparg);
  1187.                     INCREF(w);
  1188.                     PUSH(w);
  1189.                 }
  1190.             }
  1191.             DECREF(v);
  1192.             break;
  1193.         
  1194.         case STORE_ATTR:
  1195.             w = GETNAMEV(oparg);
  1196.             v = POP();
  1197.             u = POP();
  1198.             err = setattro(v, w, u); /* v.w = u */
  1199.             DECREF(v);
  1200.             DECREF(u);
  1201.             break;
  1202.         
  1203.         case DELETE_ATTR:
  1204.             w = GETNAMEV(oparg);
  1205.             v = POP();
  1206.             err = setattro(v, w, (object *)NULL); /* del v.w */
  1207.             DECREF(v);
  1208.             break;
  1209.         
  1210.         case STORE_GLOBAL:
  1211.             w = GETNAMEV(oparg);
  1212.             v = POP();
  1213. #ifdef SUPPORT_OBSOLETE_ACCESS
  1214.             if (f->f_locals != NULL) {
  1215.                 u = dict2lookup(f->f_locals, w);
  1216.                 if (u != NULL && is_accessobject(u)) {
  1217.                     err = setaccessvalue(u, f->f_globals,
  1218.                                  v);
  1219.                     DECREF(v);
  1220.                     break;
  1221.                 }
  1222.             }
  1223. #endif
  1224.             err = dict2insert(f->f_globals, w, v);
  1225.             DECREF(v);
  1226.             break;
  1227.         
  1228.         case DELETE_GLOBAL:
  1229.             w = GETNAMEV(oparg);
  1230. #ifdef SUPPORT_OBSOLETE_ACCESS
  1231.             if (f->f_locals != NULL) {
  1232.                 u = dict2lookup(f->f_locals, w);
  1233.                 if (u != NULL && is_accessobject(u)) {
  1234.                     err = setaccessvalue(u, f->f_globals,
  1235.                                  (object *)NULL);
  1236.                     break;
  1237.                 }
  1238.             }
  1239. #endif
  1240.             if ((err = dict2remove(f->f_globals, w)) != 0)
  1241.                 err_setval(NameError, w);
  1242.             break;
  1243.         
  1244.         case LOAD_CONST:
  1245.             x = GETCONST(oparg);
  1246.             INCREF(x);
  1247.             PUSH(x);
  1248.             break;
  1249.         
  1250.         case LOAD_NAME:
  1251.             w = GETNAMEV(oparg);
  1252.             if ((x = f->f_locals) == NULL) {
  1253.                 err_setstr(SystemError, "no locals");
  1254.                 break;
  1255.             }
  1256.             x = dict2lookup(x, w);
  1257.             if (x == NULL) {
  1258.                 err_clear();
  1259.                 x = dict2lookup(f->f_globals, w);
  1260.                 if (x == NULL) {
  1261.                     err_clear();
  1262.                     x = dict2lookup(f->f_builtins, w);
  1263.                     if (x == NULL) {
  1264.                         err_setval(NameError, w);
  1265.                         break;
  1266.                     }
  1267.                 }
  1268.             }
  1269. #ifdef SUPPORT_OBSOLETE_ACCESS
  1270.             if (is_accessobject(x)) {
  1271.                 x = getaccessvalue(x, f->f_globals /* XXX */);
  1272.                 if (x == NULL)
  1273.                     break;
  1274.             }
  1275.             else
  1276. #endif
  1277.                 INCREF(x);
  1278.             PUSH(x);
  1279.             break;
  1280.         
  1281.         case LOAD_GLOBAL:
  1282.             w = GETNAMEV(oparg);
  1283.             x = dict2lookup(f->f_globals, w);
  1284.             if (x == NULL) {
  1285.                 err_clear();
  1286.                 x = dict2lookup(f->f_builtins, w);
  1287.                 if (x == NULL) {
  1288.                     err_setval(NameError, w);
  1289.                     break;
  1290.                 }
  1291.             }
  1292. #ifdef SUPPORT_OBSOLETE_ACCESS
  1293.             if (is_accessobject(x)) {
  1294.                 x = getaccessvalue(x, f->f_globals);
  1295.                 if (x == NULL)
  1296.                     break;
  1297.             }
  1298.             else
  1299. #endif
  1300.                 INCREF(x);
  1301.             PUSH(x);
  1302.             break;
  1303.  
  1304. #if 0
  1305.         case LOAD_LOCAL:
  1306.             w = GETNAMEV(oparg);
  1307.             if ((x = f->f_locals) == NULL) {
  1308.                 err_setstr(SystemError, "no locals");
  1309.                 break;
  1310.             }
  1311.             x = dict2lookup(x, w);
  1312.             if (x == NULL) {
  1313.                 err_setval(NameError, w);
  1314.                 break;
  1315.             }
  1316.             if (is_accessobject(x)) {
  1317.                 x = getaccessvalue(x, f->f_locals);
  1318.                 if (x == NULL)
  1319.                     break;
  1320.             }
  1321.             else
  1322.                 INCREF(x);
  1323.             PUSH(x);
  1324.             break;
  1325. #endif
  1326.  
  1327.         case LOAD_FAST:
  1328.             x = GETLOCAL(oparg);
  1329.             if (x == NULL) {
  1330.                 err_setval(NameError,
  1331.                        gettupleitem(co->co_varnames,
  1332.                             oparg));
  1333.                 break;
  1334.             }
  1335. #ifdef SUPPORT_OBSOLETE_ACCESS
  1336.             if (is_accessobject(x)) {
  1337.                 x = getaccessvalue(x, f->f_locals);
  1338.                 if (x == NULL)
  1339.                     break;
  1340.             }
  1341.             else
  1342. #endif
  1343.                 INCREF(x);
  1344.             PUSH(x);
  1345.             break;
  1346.  
  1347.         case STORE_FAST:
  1348.             v = POP();
  1349. #ifdef SUPPORT_OBSOLETE_ACCESS
  1350.             w = GETLOCAL(oparg);
  1351.             if (w != NULL && is_accessobject(w)) {
  1352.                 err = setaccessvalue(w, f->f_locals, v);
  1353.                 DECREF(v);
  1354.                 break;
  1355.             }
  1356. #endif
  1357.             SETLOCAL(oparg, v);
  1358.             break;
  1359.  
  1360.         case DELETE_FAST:
  1361. #ifdef SUPPORT_OBSOLETE_ACCESS
  1362.             x = GETLOCAL(oparg);
  1363.             if (x == NULL) {
  1364.                 err_setval(NameError,
  1365.                        gettupleitem(co->co_varnames,
  1366.                             oparg));
  1367.                 break;
  1368.             }
  1369.             if (is_accessobject(x)) {
  1370.                 err = setaccessvalue(x, f->f_locals,
  1371.                              (object *)NULL);
  1372.                 break;
  1373.             }
  1374. #endif
  1375.             SETLOCAL(oparg, NULL);
  1376.             break;
  1377.         
  1378.         case BUILD_TUPLE:
  1379.             x = newtupleobject(oparg);
  1380.             if (x != NULL) {
  1381.                 for (; --oparg >= 0;) {
  1382.                     w = POP();
  1383.                     SETTUPLEITEM(x, oparg, w);
  1384.                 }
  1385.                 PUSH(x);
  1386.             }
  1387.             break;
  1388.         
  1389.         case BUILD_LIST:
  1390.             x =  newlistobject(oparg);
  1391.             if (x != NULL) {
  1392.                 for (; --oparg >= 0;) {
  1393.                     w = POP();
  1394.                     err = setlistitem(x, oparg, w);
  1395.                     if (err != 0)
  1396.                         break;
  1397.                 }
  1398.                 PUSH(x);
  1399.             }
  1400.             break;
  1401.         
  1402.         case BUILD_MAP:
  1403.             x = newdictobject();
  1404.             PUSH(x);
  1405.             break;
  1406.         
  1407.         case LOAD_ATTR:
  1408.             w = GETNAMEV(oparg);
  1409.             v = POP();
  1410.             x = getattro(v, w);
  1411.             DECREF(v);
  1412.             PUSH(x);
  1413.             break;
  1414.         
  1415.         case COMPARE_OP:
  1416.             w = POP();
  1417.             v = POP();
  1418.             x = cmp_outcome(oparg, v, w);
  1419.             DECREF(v);
  1420.             DECREF(w);
  1421.             PUSH(x);
  1422.             break;
  1423.         
  1424.         case IMPORT_NAME:
  1425.             w = GETNAMEV(oparg);
  1426.             x = dictlookup(f->f_builtins, "__import__");
  1427.             if (x == NULL) {
  1428.                 err_setstr(ImportError,
  1429.                        "__import__ not found");
  1430.                 break;
  1431.             }
  1432.             if (is_methodobject(x)) {
  1433.                 u = None;
  1434.                 INCREF(u);
  1435.             }
  1436.             else {
  1437.                 u = find_from_args(f, INSTR_OFFSET());
  1438.                 if (u == NULL) {
  1439.                     x = u;
  1440.                     break;
  1441.                 }
  1442.             }
  1443.             w = mkvalue("(OOOO)",
  1444.                     w,
  1445.                     f->f_globals,
  1446.                     f->f_locals == NULL ? None : f->f_locals,
  1447.                     u);
  1448.             DECREF(u);
  1449.             if (w == NULL) {
  1450.                 x = NULL;
  1451.                 break;
  1452.             }
  1453.             x = call_object(x, w);
  1454.             DECREF(w);
  1455.             PUSH(x);
  1456.             break;
  1457.         
  1458.         case IMPORT_FROM:
  1459.             w = GETNAMEV(oparg);
  1460.             v = TOP();
  1461.             fast_2_locals(f);
  1462.             if ((x = f->f_locals) == NULL) {
  1463.                 err_setstr(SystemError, "no locals");
  1464.                 break;
  1465.             }
  1466.             err = import_from(x, v, w);
  1467.             locals_2_fast(f, 0);
  1468.             break;
  1469.  
  1470. #ifdef SUPPORT_OBSOLETE_ACCESS
  1471.         case ACCESS_MODE:
  1472.             v = POP();
  1473.             w = GETNAMEV(oparg);
  1474.             if (getstringvalue(w)[0] == '*')
  1475.                 defmode = getintvalue(v);
  1476.             else
  1477.                 err = access_statement(w, v, f);
  1478.             DECREF(v);
  1479.             break;
  1480. #endif
  1481.         
  1482.         case JUMP_FORWARD:
  1483.             JUMPBY(oparg);
  1484.             break;
  1485.         
  1486.         case JUMP_IF_FALSE:
  1487.             err = testbool(TOP());
  1488.             if (err > 0)
  1489.                 err = 0;
  1490.             else if (err == 0)
  1491.                 JUMPBY(oparg);
  1492.             break;
  1493.         
  1494.         case JUMP_IF_TRUE:
  1495.             err = testbool(TOP());
  1496.             if (err > 0) {
  1497.                 err = 0;
  1498.                 JUMPBY(oparg);
  1499.             }
  1500.             break;
  1501.         
  1502.         case JUMP_ABSOLUTE:
  1503.             JUMPTO(oparg);
  1504.             break;
  1505.         
  1506.         case FOR_LOOP:
  1507.             /* for v in s: ...
  1508.                On entry: stack contains s, i.
  1509.                On exit: stack contains s, i+1, s[i];
  1510.                but if loop exhausted:
  1511.                    s, i are popped, and we jump */
  1512.             w = POP(); /* Loop index */
  1513.             v = POP(); /* Sequence object */
  1514.             u = loop_subscript(v, w);
  1515.             if (u != NULL) {
  1516.                 PUSH(v);
  1517.                 x = newintobject(getintvalue(w)+1);
  1518.                 PUSH(x);
  1519.                 DECREF(w);
  1520.                 PUSH(u);
  1521.             }
  1522.             else {
  1523.                 DECREF(v);
  1524.                 DECREF(w);
  1525.                 /* A NULL can mean "s exhausted"
  1526.                    but also an error: */
  1527.                 if (err_occurred())
  1528.                     why = WHY_EXCEPTION;
  1529.                 else
  1530.                     JUMPBY(oparg);
  1531.             }
  1532.             break;
  1533.         
  1534.         case SETUP_LOOP:
  1535.         case SETUP_EXCEPT:
  1536.         case SETUP_FINALLY:
  1537.             setup_block(f, opcode, INSTR_OFFSET() + oparg,
  1538.                         STACK_LEVEL());
  1539.             break;
  1540.         
  1541.         case SET_LINENO:
  1542. #ifdef LLTRACE
  1543.             if (lltrace)
  1544.                 printf("--- %s:%d \n", filename, oparg);
  1545. #endif
  1546.             f->f_lineno = oparg;
  1547.             if (f->f_trace != NULL) {
  1548.                 /* Trace each line of code reached */
  1549.                 f->f_lasti = INSTR_OFFSET();
  1550.                 err = call_trace(&f->f_trace, &f->f_trace,
  1551.                          f, "line", None);
  1552.             }
  1553.             break;
  1554.  
  1555.         case CALL_FUNCTION:
  1556.         {
  1557.             int na = oparg & 0xff;
  1558.             int nk = (oparg>>8) & 0xff;
  1559.             int n = na + 2*nk;
  1560.             object **pfunc = stack_pointer - n - 1;
  1561.             object *func = *pfunc;
  1562.             object *self = NULL;
  1563.             object *class = NULL;
  1564.             f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
  1565.             if (is_instancemethodobject(func)) {
  1566.                 self = instancemethodgetself(func);
  1567.                 class = instancemethodgetclass(func);
  1568.                 func = instancemethodgetfunc(func);
  1569.                 INCREF(func);
  1570.                 if (self != NULL) {
  1571.                     INCREF(self);
  1572.                     DECREF(*pfunc);
  1573.                     *pfunc = self;
  1574.                     na++;
  1575.                     n++;
  1576.                 }
  1577.                 else {
  1578.                     /* Unbound methods must be
  1579.                        called with an instance of
  1580.                        the class (or a derived
  1581.                        class) as first argument */
  1582.                     if (na > 0 &&
  1583.                         (self = stack_pointer[-n])
  1584.                          != NULL &&
  1585.                         is_instanceobject(self) &&
  1586.                         issubclass(
  1587.                             (object *)
  1588.                             (((instanceobject *)self)
  1589.                              ->in_class),
  1590.                             class))
  1591.                         /* Handy-dandy */ ;
  1592.                     else {
  1593.                         err_setstr(TypeError,
  1594.        "unbound method must be called with class instance 1st argument");
  1595.                         x = NULL;
  1596.                         break;
  1597.                     }
  1598.                 }
  1599.             }
  1600.             else
  1601.                 INCREF(func);
  1602.             if (is_funcobject(func)) {
  1603.                 object *co = getfunccode(func);
  1604.                 object *globals = getfuncglobals(func);
  1605.                 object *argdefs = PyFunction_GetDefaults(func);
  1606.                 object **d;
  1607.                 int nd;
  1608.                 if (argdefs != NULL) {
  1609.                     d = &GETTUPLEITEM(argdefs, 0);
  1610.                     nd = ((tupleobject *)argdefs)->ob_size;
  1611.                 }
  1612.                 else {
  1613.                     d = NULL;
  1614.                     nd = 0;
  1615.                 }
  1616.                 x = eval_code2(
  1617.                     (codeobject *)co,
  1618.                     globals, (object *)NULL,
  1619.                     stack_pointer-n, na,
  1620.                     stack_pointer-2*nk, nk,
  1621.                     d, nd,
  1622.                     class);
  1623.             }
  1624.             else {
  1625.                 object *args = newtupleobject(na);
  1626.                 object *kwdict = NULL;
  1627.                 if (args == NULL) {
  1628.                     x = NULL;
  1629.                     break;
  1630.                 }
  1631.                 if (nk > 0) {
  1632.                     kwdict = newdictobject();
  1633.                     if (kwdict == NULL) {
  1634.                         x = NULL;
  1635.                         break;
  1636.                     }
  1637.                     err = 0;
  1638.                     while (--nk >= 0) {
  1639.                         object *value = POP();
  1640.                         object *key = POP();
  1641.                         err = mappinginsert(
  1642.                             kwdict, key, value);
  1643.                         if (err) {
  1644.                             DECREF(key);
  1645.                             DECREF(value);
  1646.                             break;
  1647.                         }
  1648.                     }
  1649.                     if (err) {
  1650.                         DECREF(args);
  1651.                         DECREF(kwdict);
  1652.                         break;
  1653.                     }
  1654.                 }
  1655.                 while (--na >= 0) {
  1656.                     w = POP();
  1657.                     SETTUPLEITEM(args, na, w);
  1658.                 }
  1659.                 x = PyEval_CallObjectWithKeywords(
  1660.                     func, args, kwdict);
  1661.                 DECREF(args);
  1662.                 XDECREF(kwdict);
  1663.             }
  1664.             DECREF(func);
  1665.             while (stack_pointer > pfunc) {
  1666.                 w = POP();
  1667.                 DECREF(w);
  1668.             }
  1669.             PUSH(x);
  1670.             break;
  1671.         }
  1672.         
  1673.         case MAKE_FUNCTION:
  1674.             v = POP(); /* code object */
  1675.             x = newfuncobject(v, f->f_globals);
  1676.             DECREF(v);
  1677.             /* XXX Maybe this should be a separate opcode? */
  1678.             if (x != NULL && oparg > 0) {
  1679.                 v = newtupleobject(oparg);
  1680.                 if (v == NULL) {
  1681.                     DECREF(x);
  1682.                     x = NULL;
  1683.                     break;
  1684.                 }
  1685.                 while (--oparg >= 0) {
  1686.                     w = POP();
  1687.                     SETTUPLEITEM(v, oparg, w);
  1688.                 }
  1689.                 err = PyFunction_SetDefaults(x, v);
  1690.                 DECREF(v);
  1691.             }
  1692.             PUSH(x);
  1693.             break;
  1694.  
  1695.         case BUILD_SLICE:
  1696.             if (oparg == 3)
  1697.                 w = POP();
  1698.             else
  1699.                 w = NULL;
  1700.             v = POP();
  1701.             u = POP();
  1702.             x = build_slice(u,v,w);
  1703.             DECREF(u);
  1704.             DECREF(v);
  1705.             XDECREF(w);
  1706.             PUSH(x);
  1707.             break;
  1708.  
  1709.  
  1710.         default:
  1711.             fprintf(stderr,
  1712.                 "XXX lineno: %d, opcode: %d\n",
  1713.                 f->f_lineno, opcode);
  1714.             err_setstr(SystemError, "unknown opcode");
  1715.             why = WHY_EXCEPTION;
  1716.             break;
  1717.  
  1718. #ifdef CASE_TOO_BIG
  1719.         }
  1720. #endif
  1721.  
  1722.         } /* switch */
  1723.  
  1724.         on_error:
  1725.         
  1726.         /* Quickly continue if no error occurred */
  1727.         
  1728.         if (why == WHY_NOT) {
  1729.             if (err == 0 && x != NULL) {
  1730. #ifdef CHECKEXC
  1731.                 if (err_occurred())
  1732.                     fprintf(stderr,
  1733.                         "XXX undetected error\n");
  1734.                 else
  1735. #endif
  1736.                     continue; /* Normal, fast path */
  1737.             }
  1738.             why = WHY_EXCEPTION;
  1739.             x = None;
  1740.             err = 0;
  1741.         }
  1742.  
  1743. #ifdef CHECKEXC
  1744.         /* Double-check exception status */
  1745.         
  1746.         if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
  1747.             if (!err_occurred()) {
  1748.                 fprintf(stderr, "XXX ghost error\n");
  1749.                 err_setstr(SystemError, "ghost error");
  1750.                 why = WHY_EXCEPTION;
  1751.             }
  1752.         }
  1753.         else {
  1754.             if (err_occurred()) {
  1755.                 fprintf(stderr,
  1756.                     "XXX undetected error (why=%d)\n",
  1757.                     why);
  1758.                 why = WHY_EXCEPTION;
  1759.             }
  1760.         }
  1761. #endif
  1762.  
  1763.         /* Log traceback info if this is a real exception */
  1764.         
  1765.         if (why == WHY_EXCEPTION) {
  1766.             f->f_lasti = INSTR_OFFSET() - 1;
  1767.             if (HAS_ARG(opcode))
  1768.                 f->f_lasti -= 2;
  1769.             tb_here(f);
  1770.  
  1771.             if (f->f_trace)
  1772.                 call_exc_trace(&f->f_trace, &f->f_trace, f);
  1773.             if (sys_profile)
  1774.                 call_exc_trace(&sys_profile, (object**)0, f);
  1775.         }
  1776.         
  1777.         /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
  1778.         
  1779.         if (why == WHY_RERAISE)
  1780.             why = WHY_EXCEPTION;
  1781.  
  1782.         /* Unwind stacks if a (pseudo) exception occurred */
  1783.         
  1784.         while (why != WHY_NOT && f->f_iblock > 0) {
  1785.             block *b = pop_block(f);
  1786.             while (STACK_LEVEL() > b->b_level) {
  1787.                 v = POP();
  1788.                 XDECREF(v);
  1789.             }
  1790.             if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
  1791.                 why = WHY_NOT;
  1792.                 JUMPTO(b->b_handler);
  1793.                 break;
  1794.             }
  1795.             if (b->b_type == SETUP_FINALLY ||
  1796.                     b->b_type == SETUP_EXCEPT &&
  1797.                     why == WHY_EXCEPTION) {
  1798.                 if (why == WHY_EXCEPTION) {
  1799.                     object *exc, *val, *tb;
  1800.                     err_fetch(&exc, &val, &tb);
  1801.                     if (val == NULL) {
  1802.                         val = None;
  1803.                         INCREF(val);
  1804.                     }
  1805.                     /* Make the raw exception data
  1806.                        available to the handler,
  1807.                        so a program can emulate the
  1808.                        Python main loop.  Don't do
  1809.                        this for 'finally'. */
  1810.                     if (b->b_type == SETUP_EXCEPT) {
  1811.                         sysset("exc_traceback", tb);
  1812.                         sysset("exc_value", val);
  1813.                         sysset("exc_type", exc);
  1814.                     }
  1815.                     PUSH(tb);
  1816.                     PUSH(val);
  1817.                     PUSH(exc);
  1818.                 }
  1819.                 else {
  1820.                     if (why == WHY_RETURN)
  1821.                         PUSH(retval);
  1822.                     v = newintobject((long)why);
  1823.                     PUSH(v);
  1824.                 }
  1825.                 why = WHY_NOT;
  1826.                 JUMPTO(b->b_handler);
  1827.                 break;
  1828.             }
  1829.         } /* unwind stack */
  1830.  
  1831.         /* End the loop if we still have an error (or return) */
  1832.         
  1833.         if (why != WHY_NOT)
  1834.             break;
  1835.         
  1836.     } /* main loop */
  1837.     
  1838.     /* Pop remaining stack entries */
  1839.     
  1840.     while (!EMPTY()) {
  1841.         v = POP();
  1842.         XDECREF(v);
  1843.     }
  1844.     
  1845.     if (why != WHY_RETURN)
  1846.         retval = NULL;
  1847.     
  1848.     if (f->f_trace) {
  1849.         if (why == WHY_RETURN) {
  1850.             if (call_trace(&f->f_trace, &f->f_trace, f,
  1851.                        "return", retval)) {
  1852.                 XDECREF(retval);
  1853.                 retval = NULL;
  1854.                 why = WHY_EXCEPTION;
  1855.             }
  1856.         }
  1857.     }
  1858.     
  1859.     if (sys_profile && why == WHY_RETURN) {
  1860.         if (call_trace(&sys_profile, (object**)0,
  1861.                    f, "return", retval)) {
  1862.             XDECREF(retval);
  1863.             retval = NULL;
  1864.             why = WHY_EXCEPTION;
  1865.         }
  1866.     }
  1867.     
  1868.     /* Restore previous frame and release the current one */
  1869.  
  1870.     current_frame = f->f_back;
  1871.     DECREF(f);
  1872.     --recursion_depth;
  1873.     
  1874.     return retval;
  1875. }
  1876.  
  1877. #ifdef LLTRACE
  1878. static int
  1879. prtrace(v, str)
  1880.     object *v;
  1881.     char *str;
  1882. {
  1883.     printf("%s ", str);
  1884.     if (printobject(v, stdout, 0) != 0)
  1885.         err_clear(); /* Don't know what else to do */
  1886.     printf("\n");
  1887. }
  1888. #endif
  1889.  
  1890. static void
  1891. call_exc_trace(p_trace, p_newtrace, f)
  1892.     object **p_trace, **p_newtrace;
  1893.     frameobject *f;
  1894. {
  1895.     object *type, *value, *traceback, *arg;
  1896.     int err;
  1897.     err_fetch(&type, &value, &traceback);
  1898.     if (value == NULL) {
  1899.         value = None;
  1900.         INCREF(value);
  1901.     }
  1902.     arg = mkvalue("(OOO)", type, value, traceback);
  1903.     if (arg == NULL) {
  1904.         err_restore(type, value, traceback);
  1905.         return;
  1906.     }
  1907.     err = call_trace(p_trace, p_newtrace, f, "exception", arg);
  1908.     DECREF(arg);
  1909.     if (err == 0)
  1910.         err_restore(type, value, traceback);
  1911.     else {
  1912.         XDECREF(type);
  1913.         XDECREF(value);
  1914.         XDECREF(traceback);
  1915.     }
  1916. }
  1917.  
  1918. static int
  1919. call_trace(p_trace, p_newtrace, f, msg, arg)
  1920.     object **p_trace; /* in/out; may not be NULL;
  1921.                  may not point to NULL variable initially */
  1922.     object **p_newtrace; /* in/out; may be NULL;
  1923.                 may point to NULL variable;
  1924.                 may be same variable as p_newtrace */
  1925.     frameobject *f;
  1926.     char *msg;
  1927.     object *arg;
  1928. {
  1929.     object *args, *what;
  1930.     object *res = NULL;
  1931.     static int tracing = 0;
  1932.     
  1933.     if (tracing) {
  1934.         /* Don't do recursive traces */
  1935.         if (p_newtrace) {
  1936.             XDECREF(*p_newtrace);
  1937.             *p_newtrace = NULL;
  1938.         }
  1939.         return 0;
  1940.     }
  1941.     
  1942.     args = newtupleobject(3);
  1943.     if (args == NULL)
  1944.         goto cleanup;
  1945.     what = newstringobject(msg);
  1946.     if (what == NULL)
  1947.         goto cleanup;
  1948.     INCREF(f);
  1949.     SETTUPLEITEM(args, 0, (object *)f);
  1950.     SETTUPLEITEM(args, 1, what);
  1951.     if (arg == NULL)
  1952.         arg = None;
  1953.     INCREF(arg);
  1954.     SETTUPLEITEM(args, 2, arg);
  1955.     tracing++;
  1956.     fast_2_locals(f);
  1957.     res = call_object(*p_trace, args); /* May clear *p_trace! */
  1958.     locals_2_fast(f, 1);
  1959.     tracing--;
  1960.  cleanup:
  1961.     XDECREF(args);
  1962.     if (res == NULL) {
  1963.         /* The trace proc raised an exception */
  1964.         tb_here(f);
  1965.         XDECREF(*p_trace);
  1966.         *p_trace = NULL;
  1967.         if (p_newtrace) {
  1968.             XDECREF(*p_newtrace);
  1969.             *p_newtrace = NULL;
  1970.         }
  1971.         return -1;
  1972.     }
  1973.     else {
  1974.         if (p_newtrace) {
  1975.             XDECREF(*p_newtrace);
  1976.             if (res == None)
  1977.                 *p_newtrace = NULL;
  1978.             else {
  1979.                 INCREF(res);
  1980.                 *p_newtrace = res;
  1981.             }
  1982.         }
  1983.         DECREF(res);
  1984.         return 0;
  1985.     }
  1986. }
  1987.  
  1988. object *
  1989. getbuiltins()
  1990. {
  1991.     if (current_frame == NULL)
  1992.         return getbuiltinmod();
  1993.     else
  1994.         return current_frame->f_builtins;
  1995. }
  1996.  
  1997. object *
  1998. getlocals()
  1999. {
  2000.     if (current_frame == NULL)
  2001.         return NULL;
  2002.     fast_2_locals(current_frame);
  2003.     return current_frame->f_locals;
  2004. }
  2005.  
  2006. object *
  2007. getglobals()
  2008. {
  2009.     if (current_frame == NULL)
  2010.         return NULL;
  2011.     else
  2012.         return current_frame->f_globals;
  2013. }
  2014.  
  2015. object *
  2016. getowner()
  2017. {
  2018.     if (current_frame == NULL)
  2019.         return NULL;
  2020.     else
  2021.         return current_frame->f_owner;
  2022. }
  2023.  
  2024. object *
  2025. getframe()
  2026. {
  2027.     return (object *)current_frame;
  2028. }
  2029.  
  2030. int
  2031. getrestricted()
  2032. {
  2033.     return current_frame == NULL ? 0 : current_frame->f_restricted;
  2034. }
  2035.  
  2036. void
  2037. flushline()
  2038. {
  2039.     object *f = sysget("stdout");
  2040.     if (softspace(f, 0))
  2041.         writestring("\n", f);
  2042. }
  2043.  
  2044.  
  2045. #define BINOP(opname, ropname, thisfunc) \
  2046.     if (!is_instanceobject(v) && !is_instanceobject(w)) \
  2047.         ; \
  2048.     else \
  2049.         return instancebinop(v, w, opname, ropname, thisfunc)
  2050.  
  2051.  
  2052. static object *
  2053. or(v, w)
  2054.     object *v, *w;
  2055. {
  2056.     BINOP("__or__", "__ror__", or);
  2057.     if (v->ob_type->tp_as_number != NULL) {
  2058.         object *x;
  2059.         object * (*f) FPROTO((object *, object *));
  2060.         if (coerce(&v, &w) != 0)
  2061.             return NULL;
  2062.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  2063.             x = (*f)(v, w);
  2064.         DECREF(v);
  2065.         DECREF(w);
  2066.         if (f != NULL)
  2067.             return x;
  2068.     }
  2069.     err_setstr(TypeError, "bad operand type(s) for |");
  2070.     return NULL;
  2071. }
  2072.  
  2073. static object *
  2074. xor(v, w)
  2075.     object *v, *w;
  2076. {
  2077.     BINOP("__xor__", "__rxor__", xor);
  2078.     if (v->ob_type->tp_as_number != NULL) {
  2079.         object *x;
  2080.         object * (*f) FPROTO((object *, object *));
  2081.         if (coerce(&v, &w) != 0)
  2082.             return NULL;
  2083.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  2084.             x = (*f)(v, w);
  2085.         DECREF(v);
  2086.         DECREF(w);
  2087.         if (f != NULL)
  2088.             return x;
  2089.     }
  2090.     err_setstr(TypeError, "bad operand type(s) for ^");
  2091.     return NULL;
  2092. }
  2093.  
  2094. static object *
  2095. and(v, w)
  2096.     object *v, *w;
  2097. {
  2098.     BINOP("__and__", "__rand__", and);
  2099.     if (v->ob_type->tp_as_number != NULL) {
  2100.         object *x;
  2101.         object * (*f) FPROTO((object *, object *));
  2102.         if (coerce(&v, &w) != 0)
  2103.             return NULL;
  2104.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  2105.             x = (*f)(v, w);
  2106.         DECREF(v);
  2107.         DECREF(w);
  2108.         if (f != NULL)
  2109.             return x;
  2110.     }
  2111.     err_setstr(TypeError, "bad operand type(s) for &");
  2112.     return NULL;
  2113. }
  2114.  
  2115. static object *
  2116. lshift(v, w)
  2117.     object *v, *w;
  2118. {
  2119.     BINOP("__lshift__", "__rlshift__", lshift);
  2120.     if (v->ob_type->tp_as_number != NULL) {
  2121.         object *x;
  2122.         object * (*f) FPROTO((object *, object *));
  2123.         if (coerce(&v, &w) != 0)
  2124.             return NULL;
  2125.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  2126.             x = (*f)(v, w);
  2127.         DECREF(v);
  2128.         DECREF(w);
  2129.         if (f != NULL)
  2130.             return x;
  2131.     }
  2132.     err_setstr(TypeError, "bad operand type(s) for <<");
  2133.     return NULL;
  2134. }
  2135.  
  2136. static object *
  2137. rshift(v, w)
  2138.     object *v, *w;
  2139. {
  2140.     BINOP("__rshift__", "__rrshift__", rshift);
  2141.     if (v->ob_type->tp_as_number != NULL) {
  2142.         object *x;
  2143.         object * (*f) FPROTO((object *, object *));
  2144.         if (coerce(&v, &w) != 0)
  2145.             return NULL;
  2146.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  2147.             x = (*f)(v, w);
  2148.         DECREF(v);
  2149.         DECREF(w);
  2150.         if (f != NULL)
  2151.             return x;
  2152.     }
  2153.     err_setstr(TypeError, "bad operand type(s) for >>");
  2154.     return NULL;
  2155. }
  2156.  
  2157. static object *
  2158. add(v, w)
  2159.     object *v, *w;
  2160. {
  2161.     BINOP("__add__", "__radd__", add);
  2162.     if (v->ob_type->tp_as_sequence != NULL)
  2163.         return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
  2164.     else if (v->ob_type->tp_as_number != NULL) {
  2165.         object *x;
  2166.         if (coerce(&v, &w) != 0)
  2167.             return NULL;
  2168.         x = (*v->ob_type->tp_as_number->nb_add)(v, w);
  2169.         DECREF(v);
  2170.         DECREF(w);
  2171.         return x;
  2172.     }
  2173.     err_setstr(TypeError, "bad operand type(s) for +");
  2174.     return NULL;
  2175. }
  2176.  
  2177. static object *
  2178. sub(v, w)
  2179.     object *v, *w;
  2180. {
  2181.     BINOP("__sub__", "__rsub__", sub);
  2182.     if (v->ob_type->tp_as_number != NULL) {
  2183.         object *x;
  2184.         if (coerce(&v, &w) != 0)
  2185.             return NULL;
  2186.         x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
  2187.         DECREF(v);
  2188.         DECREF(w);
  2189.         return x;
  2190.     }
  2191.     err_setstr(TypeError, "bad operand type(s) for -");
  2192.     return NULL;
  2193. }
  2194.  
  2195. static object *
  2196. mul(v, w)
  2197.     object *v, *w;
  2198. {
  2199.     typeobject *tp;
  2200.     tp = v->ob_type;
  2201.     BINOP("__mul__", "__rmul__", mul);
  2202.     if (tp->tp_as_number != NULL &&
  2203.         w->ob_type->tp_as_sequence != NULL &&
  2204.         !is_instanceobject(v)) {
  2205.         /* number*sequence -- swap v and w */
  2206.         object *tmp = v;
  2207.         v = w;
  2208.         w = tmp;
  2209.         tp = v->ob_type;
  2210.     }
  2211.     if (tp->tp_as_number != NULL) {
  2212.         object *x;
  2213.         if (is_instanceobject(v)) {
  2214.             /* Instances of user-defined classes get their
  2215.                other argument uncoerced, so they may
  2216.                implement sequence*number as well as
  2217.                number*number. */
  2218.             INCREF(v);
  2219.             INCREF(w);
  2220.         }
  2221.         else if (coerce(&v, &w) != 0)
  2222.             return NULL;
  2223.         x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
  2224.         DECREF(v);
  2225.         DECREF(w);
  2226.         return x;
  2227.     }
  2228.     if (tp->tp_as_sequence != NULL) {
  2229.         if (!is_intobject(w)) {
  2230.             err_setstr(TypeError,
  2231.                 "can't multiply sequence with non-int");
  2232.             return NULL;
  2233.         }
  2234.         return (*tp->tp_as_sequence->sq_repeat)
  2235.                         (v, (int)getintvalue(w));
  2236.     }
  2237.     err_setstr(TypeError, "bad operand type(s) for *");
  2238.     return NULL;
  2239. }
  2240.  
  2241. static object *
  2242. divide(v, w)
  2243.     object *v, *w;
  2244. {
  2245.     BINOP("__div__", "__rdiv__", divide);
  2246.     if (v->ob_type->tp_as_number != NULL) {
  2247.         object *x;
  2248.         if (coerce(&v, &w) != 0)
  2249.             return NULL;
  2250.         x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
  2251.         DECREF(v);
  2252.         DECREF(w);
  2253.         return x;
  2254.     }
  2255.     err_setstr(TypeError, "bad operand type(s) for /");
  2256.     return NULL;
  2257. }
  2258.  
  2259. static object *
  2260. mod(v, w)
  2261.     object *v, *w;
  2262. {
  2263.     if (is_stringobject(v)) {
  2264.         return formatstring(v, w);
  2265.     }
  2266.     BINOP("__mod__", "__rmod__", mod);
  2267.     if (v->ob_type->tp_as_number != NULL) {
  2268.         object *x;
  2269.         if (coerce(&v, &w) != 0)
  2270.             return NULL;
  2271.         x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
  2272.         DECREF(v);
  2273.         DECREF(w);
  2274.         return x;
  2275.     }
  2276.     err_setstr(TypeError, "bad operand type(s) for %");
  2277.     return NULL;
  2278. }
  2279.  
  2280. static object *
  2281. powerop(v, w)
  2282.     object *v, *w;
  2283. {
  2284.     object *res;
  2285.     BINOP("__pow__", "__rpow__", powerop);
  2286.     if (v->ob_type->tp_as_number == NULL ||
  2287.         w->ob_type->tp_as_number == NULL) {
  2288.         err_setstr(TypeError, "pow() requires numeric arguments");
  2289.         return NULL;
  2290.     }
  2291.     if (coerce(&v, &w) != 0)
  2292.         return NULL;
  2293.     res = (*v->ob_type->tp_as_number->nb_power)(v, w, None);
  2294.     DECREF(v);
  2295.     DECREF(w);
  2296.     return res;
  2297. }
  2298.  
  2299. static object *
  2300. neg(v)
  2301.     object *v;
  2302. {
  2303.     if (v->ob_type->tp_as_number != NULL)
  2304.         return (*v->ob_type->tp_as_number->nb_negative)(v);
  2305.     err_setstr(TypeError, "bad operand type(s) for unary -");
  2306.     return NULL;
  2307. }
  2308.  
  2309. static object *
  2310. pos(v)
  2311.     object *v;
  2312. {
  2313.     if (v->ob_type->tp_as_number != NULL)
  2314.         return (*v->ob_type->tp_as_number->nb_positive)(v);
  2315.     err_setstr(TypeError, "bad operand type(s) for unary +");
  2316.     return NULL;
  2317. }
  2318.  
  2319. static object *
  2320. invert(v)
  2321.     object *v;
  2322. {
  2323.     object * (*f) FPROTO((object *));
  2324.     if (v->ob_type->tp_as_number != NULL &&
  2325.         (f = v->ob_type->tp_as_number->nb_invert) != NULL)
  2326.         return (*f)(v);
  2327.     err_setstr(TypeError, "bad operand type(s) for unary ~");
  2328.     return NULL;
  2329. }
  2330.  
  2331. static object *
  2332. not(v)
  2333.     object *v;
  2334. {
  2335.     int outcome = testbool(v);
  2336.     object *w;
  2337.     if (outcome < 0)
  2338.         return NULL;
  2339.     if (outcome == 0)
  2340.         w = True;
  2341.     else
  2342.         w = False;
  2343.     INCREF(w);
  2344.     return w;
  2345. }
  2346.  
  2347.  
  2348. /* External interface to call any callable object.
  2349.    The arg must be a tuple or NULL. */
  2350.  
  2351. object *
  2352. call_object(func, arg)
  2353.     object *func;
  2354.     object *arg;
  2355. {
  2356.     return PyEval_CallObjectWithKeywords(func, arg, (object *)NULL);
  2357. }
  2358.  
  2359. object *
  2360. PyEval_CallObjectWithKeywords(func, arg, kw)
  2361.     object *func;
  2362.     object *arg;
  2363.     object *kw;
  2364. {
  2365.         ternaryfunc call;
  2366.         object *result;
  2367.  
  2368.     if (arg == NULL)
  2369.         arg = newtupleobject(0);
  2370.     else if (!is_tupleobject(arg)) {
  2371.         err_setstr(TypeError, "argument list must be a tuple");
  2372.         return NULL;
  2373.     }
  2374.     else
  2375.         INCREF(arg);
  2376.  
  2377.     if (kw != NULL && !is_dictobject(kw)) {
  2378.         err_setstr(TypeError, "keyword list must be a dictionary");
  2379.         return NULL;
  2380.     }
  2381.  
  2382.         if (call = func->ob_type->tp_call)
  2383.                 result = (*call)(func, arg, kw);
  2384.         else if (is_instancemethodobject(func) || is_funcobject(func))
  2385.         result = call_function(func, arg, kw);
  2386.     else
  2387.         result = call_builtin(func, arg, kw);
  2388.  
  2389.     DECREF(arg);
  2390.     
  2391.         if (result == NULL && !err_occurred())
  2392.         err_setstr(SystemError,
  2393.                "NULL result without error in call_object");
  2394.         
  2395.         return result;
  2396. }
  2397.  
  2398. static object *
  2399. call_builtin(func, arg, kw)
  2400.     object *func;
  2401.     object *arg;
  2402.     object *kw;
  2403. {
  2404.     if (is_methodobject(func)) {
  2405.         method meth = getmethod(func);
  2406.         object *self = getself(func);
  2407.         int flags = getflags(func);
  2408.         if (!(flags & METH_VARARGS)) {
  2409.             int size = gettuplesize(arg);
  2410.             if (size == 1)
  2411.                 arg = GETTUPLEITEM(arg, 0);
  2412.             else if (size == 0)
  2413.                 arg = NULL;
  2414.         }
  2415.         if (flags & METH_KEYWORDS)
  2416.             return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  2417.         if (kw != NULL && getmappingsize(kw) != 0) {
  2418.             err_setstr(TypeError,
  2419.                    "this function takes no keyword arguments");
  2420.             return NULL;
  2421.         }
  2422.         return (*meth)(self, arg);
  2423.     }
  2424.     if (is_classobject(func)) {
  2425.         return newinstanceobject(func, arg, kw);
  2426.     }
  2427.     if (is_instanceobject(func)) {
  2428.             object *res, *call = getattr(func,"__call__");
  2429.         if (call == NULL) {
  2430.             err_clear();
  2431.             err_setstr(AttributeError,
  2432.                    "no __call__ method defined");
  2433.             return NULL;
  2434.         }
  2435.         res = PyEval_CallObjectWithKeywords(call, arg, kw);
  2436.         DECREF(call);
  2437.         return res;
  2438.     }
  2439.     err_setstr(TypeError, "call of non-function");
  2440.     return NULL;
  2441. }
  2442.  
  2443. static object *
  2444. call_function(func, arg, kw)
  2445.     object *func;
  2446.     object *arg;
  2447.     object *kw;
  2448. {
  2449.     object *class = NULL; /* == owner */
  2450.     object *argdefs;
  2451.     object **d, **k;
  2452.     int nk, nd;
  2453.     object *result;
  2454.     
  2455.     if (kw != NULL && !is_dictobject(kw)) {
  2456.         err_badcall();
  2457.         return NULL;
  2458.     }
  2459.     
  2460.     if (is_instancemethodobject(func)) {
  2461.         object *self = instancemethodgetself(func);
  2462.         class = instancemethodgetclass(func);
  2463.         func = instancemethodgetfunc(func);
  2464.         if (self == NULL) {
  2465.             /* Unbound methods must be called with an instance of
  2466.                the class (or a derived class) as first argument */
  2467.             if (gettuplesize(arg) >= 1) {
  2468.                 self = GETTUPLEITEM(arg, 0);
  2469.                 if (self != NULL &&
  2470.                     is_instanceobject(self) &&
  2471.                     issubclass((object *)
  2472.                       (((instanceobject *)self)->in_class),
  2473.                            class))
  2474.                     /* Handy-dandy */ ;
  2475.                 else
  2476.                     self = NULL;
  2477.             }
  2478.             if (self == NULL) {
  2479.                 err_setstr(TypeError,
  2480.        "unbound method must be called with class instance 1st argument");
  2481.                 return NULL;
  2482.             }
  2483.             INCREF(arg);
  2484.         }
  2485.         else {
  2486.             int argcount = gettuplesize(arg);
  2487.             object *newarg = newtupleobject(argcount + 1);
  2488.             int i;
  2489.             if (newarg == NULL)
  2490.                 return NULL;
  2491.             INCREF(self);
  2492.             SETTUPLEITEM(newarg, 0, self);
  2493.             for (i = 0; i < argcount; i++) {
  2494.                 object *v = GETTUPLEITEM(arg, i);
  2495.                 XINCREF(v);
  2496.                 SETTUPLEITEM(newarg, i+1, v);
  2497.             }
  2498.             arg = newarg;
  2499.         }
  2500.     }
  2501.     else {
  2502.         if (!is_funcobject(func)) {
  2503.             err_setstr(TypeError, "call of non-function");
  2504.             return NULL;
  2505.         }
  2506.         INCREF(arg);
  2507.     }
  2508.     
  2509.     argdefs = PyFunction_GetDefaults(func);
  2510.     if (argdefs != NULL && is_tupleobject(argdefs)) {
  2511.         d = &GETTUPLEITEM((tupleobject *)argdefs, 0);
  2512.         nd = gettuplesize(argdefs);
  2513.     }
  2514.     else {
  2515.         d = NULL;
  2516.         nd = 0;
  2517.     }
  2518.     
  2519.     if (kw != NULL) {
  2520.         int pos, i;
  2521.         nk = getmappingsize(kw);
  2522.         k = NEW(object *, 2*nk);
  2523.         if (k == NULL) {
  2524.             err_nomem();
  2525.             DECREF(arg);
  2526.             return NULL;
  2527.         }
  2528.         pos = i = 0;
  2529.         while (mappinggetnext(kw, &pos, &k[i], &k[i+1]))
  2530.             i += 2;
  2531.         nk = i/2;
  2532.         /* XXX This is broken if the caller deletes dict items! */
  2533.     }
  2534.     else {
  2535.         k = NULL;
  2536.         nk = 0;
  2537.     }
  2538.     
  2539.     result = eval_code2(
  2540.         (codeobject *)getfunccode(func),
  2541.         getfuncglobals(func), (object *)NULL,
  2542.         &GETTUPLEITEM(arg, 0), gettuplesize(arg),
  2543.         k, nk,
  2544.         d, nd,
  2545.         class);
  2546.     
  2547.     DECREF(arg);
  2548.     XDEL(k);
  2549.     
  2550.     return result;
  2551. }
  2552.  
  2553. #define SLICE_ERROR_MSG \
  2554.     "standard sequence type does not support step size other than one"
  2555.  
  2556. static object *
  2557. apply_subscript(v, w)
  2558.     object *v, *w;
  2559. {
  2560.     typeobject *tp = v->ob_type;
  2561.     if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
  2562.         err_setstr(TypeError, "unsubscriptable object");
  2563.         return NULL;
  2564.     }
  2565.     if (tp->tp_as_mapping != NULL) {
  2566.         return (*tp->tp_as_mapping->mp_subscript)(v, w);
  2567.     }
  2568.     else {
  2569.         int i;
  2570.         if (!is_intobject(w)) {          
  2571.             if (PySlice_Check(w)) {
  2572.                     err_setstr(ValueError, SLICE_ERROR_MSG); 
  2573.             } else {
  2574.                 err_setstr(TypeError,
  2575.                        "sequence subscript not int");
  2576.             }    
  2577.             return NULL;
  2578.         }
  2579.         i = getintvalue(w);
  2580.         if (i < 0) {
  2581.             int len = (*tp->tp_as_sequence->sq_length)(v);
  2582.             if (len < 0)
  2583.                 return NULL;
  2584.             i += len;
  2585.         }
  2586.         return (*tp->tp_as_sequence->sq_item)(v, i);
  2587.     }
  2588. }
  2589.  
  2590. static object *
  2591. loop_subscript(v, w)
  2592.     object *v, *w;
  2593. {
  2594.     sequence_methods *sq = v->ob_type->tp_as_sequence;
  2595.     int i;
  2596.     if (sq == NULL) {
  2597.         err_setstr(TypeError, "loop over non-sequence");
  2598.         return NULL;
  2599.     }
  2600.     i = getintvalue(w);
  2601.     v = (*sq->sq_item)(v, i);
  2602.     if (v)
  2603.         return v;
  2604.     if (err_occurred() == IndexError)
  2605.         err_clear();
  2606.     return NULL;
  2607. }
  2608.  
  2609. static int
  2610. slice_index(v, isize, pi)
  2611.     object *v;
  2612.     int isize;
  2613.     int *pi;
  2614. {
  2615.     if (v != NULL) {
  2616.         if (!is_intobject(v)) {
  2617.             err_setstr(TypeError, "slice index must be int");
  2618.             return -1;
  2619.         }
  2620.         *pi = getintvalue(v);
  2621.         if (*pi < 0)
  2622.             *pi += isize;
  2623.     }
  2624.     return 0;
  2625. }
  2626.  
  2627. static object *
  2628. build_slice(u, v, w) /* u:v:w */
  2629.     object *u, *v, *w;
  2630. {
  2631.   return PySlice_New(u,v,w);
  2632. }
  2633.  
  2634. static object *
  2635. apply_slice(u, v, w) /* return u[v:w] */
  2636.     object *u, *v, *w;
  2637. {
  2638.     typeobject *tp = u->ob_type;
  2639.     int ilow, ihigh, isize;
  2640.     if (tp->tp_as_sequence == NULL) {
  2641.         err_setstr(TypeError, "only sequences can be sliced");
  2642.         return NULL;
  2643.     }
  2644.     ilow = 0;
  2645.     isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
  2646.     if (isize < 0)
  2647.         return NULL;
  2648.     if (slice_index(v, isize, &ilow) != 0)
  2649.         return NULL;
  2650.     if (slice_index(w, isize, &ihigh) != 0)
  2651.         return NULL;
  2652.     return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
  2653. }
  2654.  
  2655. static int
  2656. assign_subscript(w, key, v) /* w[key] = v */
  2657.     object *w;
  2658.     object *key;
  2659.     object *v;
  2660. {
  2661.     typeobject *tp = w->ob_type;
  2662.     sequence_methods *sq;
  2663.     mapping_methods *mp;
  2664.     objobjargproc func1;    /** was: int (*func1)(); */
  2665.     intobjargproc func2;    /** was: int (*func2)(); */
  2666.     if ((mp = tp->tp_as_mapping) != NULL &&
  2667.             (func1 = mp->mp_ass_subscript) != NULL) {
  2668.         return (*func1)(w, key, v);
  2669.     }
  2670.     else if ((sq = tp->tp_as_sequence) != NULL &&
  2671.             (func2 = sq->sq_ass_item) != NULL) {
  2672.         if (!is_intobject(key)) {
  2673.             err_setstr(TypeError,
  2674.             "sequence subscript must be integer (assign or del)");
  2675.             return -1;
  2676.         }
  2677.         else {
  2678.             int i = getintvalue(key);
  2679.             if (i < 0) {
  2680.                 int len = (*sq->sq_length)(w);
  2681.                 if (len < 0)
  2682.                     return -1;
  2683.                 i += len;
  2684.             }
  2685.             return (*func2)(w, i, v);
  2686.         }
  2687.     }
  2688.     else {
  2689.         err_setstr(TypeError,
  2690.                 "can't assign to this subscripted object");
  2691.         return -1;
  2692.     }
  2693. }
  2694.  
  2695. static int
  2696. assign_slice(u, v, w, x) /* u[v:w] = x */
  2697.     object *u, *v, *w, *x;
  2698. {
  2699.     sequence_methods *sq = u->ob_type->tp_as_sequence;
  2700.     int ilow, ihigh, isize;
  2701.     if (sq == NULL) {
  2702.         err_setstr(TypeError, "assign to slice of non-sequence");
  2703.         return -1;
  2704.     }
  2705.     if (sq == NULL || sq->sq_ass_slice == NULL) {
  2706.         err_setstr(TypeError, "unassignable slice");
  2707.         return -1;
  2708.     }
  2709.     ilow = 0;
  2710.     isize = ihigh = (*sq->sq_length)(u);
  2711.     if (isize < 0)
  2712.         return -1;
  2713.     if (slice_index(v, isize, &ilow) != 0)
  2714.         return -1;
  2715.     if (slice_index(w, isize, &ihigh) != 0)
  2716.         return -1;
  2717.     return (*sq->sq_ass_slice)(u, ilow, ihigh, x);
  2718. }
  2719.  
  2720. static int
  2721. cmp_exception(err, v)
  2722.     object *err, *v;
  2723. {
  2724.     if (is_tupleobject(v)) {
  2725.         int i, n;
  2726.         n = gettuplesize(v);
  2727.         for (i = 0; i < n; i++) {
  2728.             /* Test recursively */
  2729.             if (cmp_exception(err, GETTUPLEITEM(v, i)))
  2730.                 return 1;
  2731.         }
  2732.         return 0;
  2733.     }
  2734.     if (is_classobject(v) && is_classobject(err))
  2735.         return issubclass(err, v);
  2736.     return err == v;
  2737. }
  2738.  
  2739. static int
  2740. cmp_member(v, w)
  2741.     object *v, *w;
  2742. {
  2743.     int i, cmp;
  2744.     object *x;
  2745.     sequence_methods *sq;
  2746.     /* Special case for char in string */
  2747.     if (is_stringobject(w)) {
  2748.         register char *s, *end;
  2749.         register char c;
  2750.         if (!is_stringobject(v) || getstringsize(v) != 1) {
  2751.             err_setstr(TypeError,
  2752.                 "string member test needs char left operand");
  2753.             return -1;
  2754.         }
  2755.         c = getstringvalue(v)[0];
  2756.         s = getstringvalue(w);
  2757.         end = s + getstringsize(w);
  2758.         while (s < end) {
  2759.             if (c == *s++)
  2760.                 return 1;
  2761.         }
  2762.         return 0;
  2763.     }
  2764.     sq = w->ob_type->tp_as_sequence;
  2765.     if (sq == NULL) {
  2766.         err_setstr(TypeError,
  2767.             "'in' or 'not in' needs sequence right argument");
  2768.         return -1;
  2769.     }
  2770.     for (i = 0; ; i++) {
  2771.         x = (*sq->sq_item)(w, i);
  2772.         if (x == NULL) {
  2773.             if (err_occurred() == IndexError) {
  2774.                 err_clear();
  2775.                 break;
  2776.             }
  2777.             return -1;
  2778.         }
  2779.         cmp = cmpobject(v, x);
  2780.         XDECREF(x);
  2781.         if (cmp == 0)
  2782.             return 1;
  2783.     }
  2784.     return 0;
  2785. }
  2786.  
  2787. static object *
  2788. cmp_outcome(op, v, w)
  2789.     int op;
  2790.     register object *v;
  2791.     register object *w;
  2792. {
  2793.     register int cmp;
  2794.     register int res = 0;
  2795.     switch (op) {
  2796.     case IS:
  2797.     case IS_NOT:
  2798.         res = (v == w);
  2799.         if (op == (int) IS_NOT)
  2800.             res = !res;
  2801.         break;
  2802.     case IN:
  2803.     case NOT_IN:
  2804.         res = cmp_member(v, w);
  2805.         if (res < 0)
  2806.             return NULL;
  2807.         if (op == (int) NOT_IN)
  2808.             res = !res;
  2809.         break;
  2810.     case EXC_MATCH:
  2811.         res = cmp_exception(v, w);
  2812.         break;
  2813.     default:
  2814.         cmp = cmpobject(v, w);
  2815.         switch (op) {
  2816.         case LT: res = cmp <  0; break;
  2817.         case LE: res = cmp <= 0; break;
  2818.         case EQ: res = cmp == 0; break;
  2819.         case NE: res = cmp != 0; break;
  2820.         case GT: res = cmp >  0; break;
  2821.         case GE: res = cmp >= 0; break;
  2822.         /* XXX no default? (res is initialized to 0 though) */
  2823.         }
  2824.     }
  2825.     v = res ? True : False;
  2826.     INCREF(v);
  2827.     return v;
  2828. }
  2829.  
  2830. static int
  2831. import_from(locals, v, name)
  2832.     object *locals;
  2833.     object *v;
  2834.     object *name;
  2835. {
  2836.     object *w, *x;
  2837.     if (!is_moduleobject(v)) {
  2838.         err_setstr(TypeError, "import-from requires module object");
  2839.         return -1;
  2840.     }
  2841.     w = getmoduledict(v);
  2842.     if (getstringvalue(name)[0] == '*') {
  2843.         int pos, err;
  2844.         object *name, *value;
  2845.         pos = 0;
  2846.         while (mappinggetnext(w, &pos, &name, &value)) {
  2847.             if (!is_stringobject(name) ||
  2848.                 getstringvalue(name)[0] == '_')
  2849.                 continue;
  2850. #ifdef SUPPORT_OBSOLETE_ACCESS
  2851.             if (is_accessobject(value)) {
  2852.                 value = getaccessvalue(value, (object *)NULL);
  2853.                 if (value == NULL) {
  2854.                     err_clear();
  2855.                     continue;
  2856.                 }
  2857.             }
  2858.             else
  2859. #endif
  2860.                 INCREF(value);
  2861.             err = dict2insert(locals, name, value);
  2862.             DECREF(value);
  2863.             if (err != 0)
  2864.                 return -1;
  2865.         }
  2866.         return 0;
  2867.     }
  2868.     else {
  2869.         x = dict2lookup(w, name);
  2870.         if (x == NULL) {
  2871.             char buf[250];
  2872.             sprintf(buf, "cannot import name %.230s",
  2873.                 getstringvalue(name));
  2874.             err_setstr(ImportError, buf);
  2875.             return -1;
  2876.         }
  2877.         else
  2878.             return dict2insert(locals, name, x);
  2879.     }
  2880. }
  2881.  
  2882. static object *
  2883. build_class(methods, bases, name)
  2884.     object *methods; /* dictionary */
  2885.     object *bases;  /* tuple containing classes */
  2886.     object *name;   /* string */
  2887. {
  2888.     int i;
  2889.     if (!is_tupleobject(bases)) {
  2890.         err_setstr(SystemError, "build_class with non-tuple bases");
  2891.         return NULL;
  2892.     }
  2893.     if (gettuplesize(bases) > 0) {
  2894.         object *base;
  2895.         base = GETTUPLEITEM(bases, 0);
  2896.         /* Call the base's *type*, if it is callable.
  2897.            This code is a hook for Donald Beaudry's type extensions.
  2898.            In unexended Python it will never be triggered since its
  2899.            types are not callable. */
  2900.         if (base->ob_type->ob_type->tp_call) {
  2901.             object *args;
  2902.             object *class;
  2903.             args = mkvalue("(OOO)", name, bases, methods);
  2904.             class = call_object((object *)base->ob_type, args);
  2905.             DECREF(args);
  2906.             return class;
  2907.         }
  2908.     }
  2909.     if (!is_dictobject(methods)) {
  2910.         err_setstr(SystemError, "build_class with non-dictionary");
  2911.         return NULL;
  2912.     }
  2913.     if (!is_stringobject(name)) {
  2914.         err_setstr(SystemError, "build_class witn non-string name");
  2915.         return NULL;
  2916.     }
  2917.     for (i = gettuplesize(bases); --i >= 0; ) {
  2918.         object *base = GETTUPLEITEM(bases, i);
  2919.         if (!is_classobject(base)) {
  2920.             err_setstr(TypeError,
  2921.                 "base is not a class object");
  2922.             return NULL;
  2923.         }
  2924.     }
  2925.     return newclassobject(bases, methods, name);
  2926. }
  2927.  
  2928. #ifdef SUPPORT_OBSOLETE_ACCESS
  2929. static int
  2930. access_statement(name, vmode, f)
  2931.     object *name;
  2932.     object *vmode;
  2933.     frameobject *f;
  2934. {
  2935.     int mode = getintvalue(vmode);
  2936.     object *value, *ac;
  2937.     typeobject *type;
  2938.     int ret;
  2939.     fast_2_locals(f);
  2940.     value = dict2lookup(f->f_locals, name);
  2941.     if (value && is_accessobject(value)) {
  2942.         err_setstr(AccessError, "can't override access");
  2943.         return -1;
  2944.     }
  2945.     err_clear();
  2946.     if (value != NULL && value != None)
  2947.         type = value->ob_type;
  2948.     else
  2949.         type = NULL;
  2950.     ac = newaccessobject(value, f->f_locals, type, mode);
  2951.     if (ac == NULL)
  2952.         return -1;
  2953.     ret = mappinginsert(f->f_locals, name, ac);
  2954.     DECREF(ac);
  2955.     locals_2_fast(f, 0);
  2956.     return ret;
  2957. }
  2958. #endif
  2959.  
  2960. static int
  2961. exec_statement(prog, globals, locals)
  2962.     object *prog;
  2963.     object *globals;
  2964.     object *locals;
  2965. {
  2966.     char *s;
  2967.     int n;
  2968.     object *v;
  2969.     int plain = 0;
  2970.  
  2971.     if (is_tupleobject(prog) && globals == None && locals == None &&
  2972.         ((n = gettuplesize(prog)) == 2 || n == 3)) {
  2973.         /* Backward compatibility hack */
  2974.         globals = gettupleitem(prog, 1);
  2975.         if (n == 3)
  2976.             locals = gettupleitem(prog, 2);
  2977.         prog = gettupleitem(prog, 0);
  2978.     }
  2979.     if (globals == None) {
  2980.         globals = getglobals();
  2981.         if (locals == None) {
  2982.             locals = getlocals();
  2983.             plain = 1;
  2984.         }
  2985.     }
  2986.     else if (locals == None)
  2987.         locals = globals;
  2988.     if (!is_stringobject(prog) &&
  2989.         !is_codeobject(prog) &&
  2990.         !is_fileobject(prog)) {
  2991.         err_setstr(TypeError,
  2992.                "exec 1st arg must be string, code or file object");
  2993.         return -1;
  2994.     }
  2995.     if (!is_dictobject(globals) || !is_dictobject(locals)) {
  2996.         err_setstr(TypeError,
  2997.             "exec 2nd/3rd args must be dict or None");
  2998.         return -1;
  2999.     }
  3000.     if (dictlookup(globals, "__builtins__") == NULL)
  3001.         dictinsert(globals, "__builtins__", current_frame->f_builtins);
  3002.     if (is_codeobject(prog)) {
  3003.         if (eval_code((codeobject *) prog, globals, locals) == NULL)
  3004.             return -1;
  3005.         return 0;
  3006.     }
  3007.     if (is_fileobject(prog)) {
  3008.         FILE *fp = getfilefile(prog);
  3009.         char *name = getstringvalue(getfilename(prog));
  3010.         if (run_file(fp, name, file_input, globals, locals) == NULL)
  3011.             return -1;
  3012.         return 0;
  3013.     }
  3014.     s = getstringvalue(prog);
  3015.     if (strlen(s) != getstringsize(prog)) {
  3016.         err_setstr(ValueError, "embedded '\\0' in exec string");
  3017.         return -1;
  3018.     }
  3019.     v = run_string(s, file_input, globals, locals);
  3020.     if (v == NULL)
  3021.         return -1;
  3022.     DECREF(v);
  3023.     if (plain)
  3024.         locals_2_fast(current_frame, 0);
  3025.     return 0;
  3026. }
  3027.  
  3028. /* Hack for newimp.py */
  3029. static object *
  3030. find_from_args(f, nexti)
  3031.     frameobject *f;
  3032.     int nexti;
  3033. {
  3034.     int opcode;
  3035.     int oparg;
  3036.     object *list, *name;
  3037.     unsigned char *next_instr;
  3038.     
  3039.     next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
  3040.     opcode = (*next_instr++);
  3041.     if (opcode != IMPORT_FROM) {
  3042.         INCREF(None);
  3043.         return None;
  3044.     }
  3045.     
  3046.     list = newlistobject(0);
  3047.     if (list == NULL)
  3048.         return NULL;
  3049.     
  3050.     do {
  3051.         oparg = (next_instr[1]<<8) + next_instr[0];
  3052.         next_instr += 2;
  3053.         name = Getnamev(f, oparg);
  3054.         if (addlistitem(list, name) < 0) {
  3055.             DECREF(list);
  3056.             break;
  3057.         }
  3058.         opcode = (*next_instr++);
  3059.     } while (opcode == IMPORT_FROM);
  3060.     
  3061.     return list;
  3062. }
  3063.